您的位置:首页 > 数据库

j2se使用数据模型和JTable完成数据库读取和查询操作

2015-03-06 16:15 651 查看
public class my_db extends JFrame implements ActionListener{

	//rowData是行数据,columnName是列名。
	
	JTable jt = null;
	JScrollPane jsp = null;
	JButton jbplus = new JButton("增加");
	JButton jbminus = new JButton("删除");
	JPanel jp1 = new JPanel();
	JPanel jp2 = new JPanel();
	JLabel jl = new JLabel("输入查询");
	JTextField jtext = new JTextField("",10);
	JButton jsearch = new JButton("查询");
	
	
	String title = "数据库操作";
	public static void main(String[] args) {
		my_db db = new my_db();

	}
	
	public my_db(){
		//创建数据模型对象
		infoModel im = new infoModel();
		//初始化JTable,装入数据模型。
		jt = new JTable(im);
		//初始化jsp
		jsp = new JScrollPane(jt);
		
		//把jsp放入JFrame
		this.add(jsp);
		jp1.add(jl);
		jp1.add(jtext);
		jp1.add(jsearch);
		jp2.add(jbplus);
		jp2.add(jbminus);
		this.add(jp1,BorderLayout.NORTH);
		this.add(jp2,BorderLayout.SOUTH);
		
		jsearch.addActionListener(this);
		jsearch.setActionCommand("search");
		jbplus.addActionListener(this);
		jbplus.setActionCommand("plus");
		jbminus.addActionListener(this);
		jbminus.setActionCommand("minus");
		
		//给窗体设置标题  
        this.setTitle(title);  
        //设置大小  
        this.setSize(640, 240);  
        
        //设置初始位置  
        this.setLocation(100, 200);  
          
        //设置当关闭窗口时jvm也退出。  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        //显示  
        this.setVisible(true);  
	}

	//实现三个按钮的监听方法
	@Override
	public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("plus")){
				System.out.println("点击了plus");
			}
			if(e.getActionCommand().equals("minus")){
				System.out.println("点击了minus");
			}
			if(e.getActionCommand().equals("search")){
				System.out.println("要进行查询");
				//查询数据库,使用数据模型,更新JTable。
				int searchValue = Integer.valueOf(this.jtext.getText().trim());
				//写sql
				String sql = "select a,b from t1 where a = '"+searchValue+"'";
				//更新moudel,使用第二个带参数的构造函数。
				infoModel im = new infoModel(sql);
				//重新装入数据
				jt.setModel(im);

			}
			
		
	}

	

}

//创建一个用于更新jtable的模型class
class infoModel extends AbstractTableModel{

	//设置显示列名,在source中查找复用方法
	@Override
	public String getColumnName(int column) {
		return (String)this.columnName.get(column);
	}

	Vector rowData,columnName;
	//定义数据库操作
		PreparedStatement ps = null;
		Connection ct = null;
		ResultSet rs = null;
		
		//定义一个初始化的方法,参数是sql语句。
		public void init(String sql){
			//如果sql是空,按默认查询,当前为查询到所有内容的语句。若不是空,则传进来的是什么就是什么。
			if(sql==""){
				sql = "select * from t1";
			}
			//初始化控件
			columnName = new Vector();
//			设置列名
			columnName.add("字段a");
			columnName.add("字段b");
			rowData = new Vector();
			
			//查询列表数据
			try {
//				加载驱动
				Class.forName("com.mysql.jdbc.Driver");
				ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jmeterdata?user=root&password=123456");
				ps = ct.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()){
					//rowData可以存放多行
					Vector hang = new Vector();
					hang.add(rs.getInt(1));
					hang.add(rs.getString(2));
					rowData.add(hang);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				try {
					if(rs!=null) rs.close();
					if(ps!=null) ps.close();
					if(ct!=null) ct.close();
				} catch (Exception e2) {
					
				}
			}
		}
		
		//	在两个构造函数里调用init方法
		//		做构造函数1,初始化数据模型
		public infoModel(){
			this.init("");
		}
	
//		构造函数2,用sql获取数据模型
		public infoModel(String sql){
			this.init(sql);
		}
		
	//下面实现数据模型类继承的AbstractTableModel的三个方法
	//得到行数
	@Override
	public int getRowCount() {
		return this.rowData.size();
	}

	//得到列数
	@Override
	public int getColumnCount() {
		return this.columnName.size();
	}

	//得到某行某列的值
	@Override
	public Object getValueAt(int row, int column) {
		return ((Vector)this.rowData.get(row)).get(column);
	}
	
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐