如何在JAVA中实现数据库和数据表的二级联动

langrisser
发布于 2020-8-31 19:42
1.4w浏览
0收藏

效果是实现的获取Mysql所有的数据库,然后单击数据库名称以获取数据库中的所有表。

它主要通过DefaultComboBoxModel实现。 思路是删除先前的模型,然后将数据重新添加到该模型,从而实现二级联动。

 

给出参考代码:

 

package com.xmlDemo.frame;
 
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
import com.xmlDemo.service.DBService;
import com.xmlDemo.service.DBToXmlService;
import com.xmlDemo.util.DBConnectionUtil;
//主要是用DefaultComboBoxModel来实现
public class ExportFrame extends JFrame implements ItemListener{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	String dbName = "xml";
 
	private List<String> list2;
	
	private String[] arrs2 = {};
	
	private DefaultComboBoxModel model;
	
	private JComboBox comboBox1;
	private JComboBox comboBox2;
	
	private final static String BASEURL="../xmlDemo/images/";
	
	private String tableName = "users";
	
	public ExportFrame(){
		try {
			export();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void export() throws Exception{
     	Image image=Toolkit.getDefaultToolkit().getImage(BASEURL+"logo.png");
 		setIconImage(image);
     	setTitle("导出信息");
     	setLayout(new FlowLayout(FlowLayout.LEFT));
     	
     	JPanel panel1 = new JPanel();
     	
     	JPanel panel2 = new JPanel();
     	
     	JLabel label1 = new JLabel("数据库:");
     	
     	List<String> list1 = new DBService().getAllDatabases();
     	
     	String arrs1[] = new String[list1.size()];
     	
     	for(int i = 0; i < list1.size(); i++){
     		arrs1[i] = list1.get(i);
     	}
     	
     	comboBox1 = new JComboBox(arrs1);
     	comboBox1.setSelectedItem(dbName);
     	comboBox1.addItemListener(this);
     	
     	panel1.add(label1);
     	panel1.add(comboBox1);
     	
     	list2 = new DBService().getAllTables(dbName);
     	
     	arrs2 = new String[list2.size()];
     	
     	for(int i = 0; i < list2.size(); i++){
     		arrs2[i] = list2.get(i);
     	}
     	
     	JLabel label2 = new JLabel("数据表:");
     	//创建一个模型
     	model = new DefaultComboBoxModel();
     	
		try {
			list2 = new DBService().getAllTables(dbName);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
     	
     	arrs2 = new String[list2.size()];
     	
     	for(int i = 0; i < list2.size(); i++){
     		arrs2[i] = list2.get(i);
     		model.addElement(arrs2[i]);
     	}
     	
     	comboBox2 = new JComboBox();
     	comboBox2.setSelectedItem(tableName);
     	comboBox2.setModel(model);
     	comboBox2.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				if(e.getStateChange()==ItemEvent.SELECTED){
					tableName = "" + e.getItem();
				}
			}
		});
     	
     	
     	panel2.add(label2);
     	panel2.add(comboBox2);
     	
     	JButton btn = new JButton("导出");
     	
     	btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					if(dbName.equals("xml")){
						new DBToXmlService().exportDataToXMlFile(dbName, tableName);
					}else{
						JOptionPane.showConfirmDialog(null, "当前版本只支持特定数据库","温馨提示",JOptionPane.YES_NO_OPTION);
					}
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
     	
     	add(panel1);
     	add(panel2);
     	add(btn);
     	
     	setVisible(true);
     	setSize(400,400);
     	setLocation(300,300);
	}
	
	
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if(e.getStateChange()==ItemEvent.SELECTED){//选中JComboBox组件
			dbName = ""+e.getItem();//获取字符串
			try {
				list2 = new DBService().getAllTables(dbName);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
	     	//@SuppressWarnings("unchecked")
			model = (DefaultComboBoxModel)comboBox2.getModel();
			//移除之前的ComboBox模型
	     	while(model.getSize()>0){
	     		model.removeElementAt(model.getSize()-1);
	     	}
	     	
	     	arrs2 = new String[list2.size()];
	     	//重新添加模型
	     	for(int i = 0;i<list2.size();i++){
	     		arrs2[i] = list2.get(i);
	     		model.addElement(arrs2[i]);
	     	}
		}
	
	}
 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.

 

数据库操作的类:

 

package com.xmlDemo.service;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.xmlDemo.util.DBConnectionUtil;
 
public class DBService {
 
//获取某个数据库的所有数据表
	public List<String> getAllTables(String databaseName) throws Exception{
		List<String> list = new ArrayList<String>();
		int i = 0;
		
		String url = "jdbc:mysql://localhost:3306/"+databaseName;
		
		Connection connection = new DBConnectionUtil().getConnection(url);
	
		try {
			ResultSet rs=connection.getMetaData().getTables("","","",null);
 
			while (rs.next()) {
				list.add(rs.getString("TABLE_NAME"));
			}
 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	public List<String> getAllDatabases() throws Exception{
		
		List<String> list = new ArrayList<String>();
		int i = 0;
		
		String sql = "show databases";
		String url="jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=UTF-8";
		Connection connection = new DBConnectionUtil().getConnection(url);
	
		try {
			PreparedStatement prepare = connection.prepareStatement(sql);
			ResultSet rs=prepare.executeQuery();
 
			while (rs.next()) {
				list.add(rs.getString(1));
			}
 
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			new DBConnectionUtil().close();
		}
 
		return list;
	}
 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.

 

如何在JAVA中实现数据库和数据表的二级联动-鸿蒙开发者社区

分类
已于2020-9-2 18:09:15修改
收藏
回复
举报


回复
    相关推荐