java语言JTable,model构造表格并显示后台数据

java语言JTable,model构造表格并显示后台数据!后台数据在这里插入图片描述

)

欢迎使用Markdown编辑器

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.DefaultTableModel;

import java.text.SimpleDateFormat;
import java.util.Date;
public class AR2 extends JFrame {//implements ActionListener

	//model.setDataVector(Object[][] rowData, Object[] columnNames); 
	 JButton button1,button2;
	Box baseBox,boxV1,boxV2;
	JScrollPane jscrollpane;
	AR listener1;
	Object[][] rowData = new Object[9][5];
	Object[] columnNames = {"学号","姓名","班级","生日","密码"};//列名 
	MyListener listener2;
	//Object[][] rowData;//搴曢儴
JFrame frame;

//public void actionPerformed(ActionEvent e) {
public void actionP() {

	//String rowData2=RowSetMetaData[][0].getText();
/******************************************杩炴帴鏁版嵁搴�******************************************************************************/
	Connection con=null;//杩炴帴鏁版嵁搴�
	java.sql.Statement sql; 
	ResultSet rs;
	try{  Class.forName("com.mysql.cj.jdbc.Driver"); //鍔犺浇JDBC_MySQL椹卞姩
	}
	catch(Exception e1){}
	String uri = "jdbc:mysql://localhost:3306/borrowers?serverTimezone=UTC&useSSL=false";
	String user ="root";
	String password ="850314";
	try{  
	con = DriverManager.getConnection(uri,user,password); //杩炴帴浠g爜
	}
	catch(SQLException e1){
		System.out.println("122222222222222222222222222222222222222222222222222222222222");
	}
	try { 
	sql=con.createStatement();
	rs=sql.executeQuery("SELECT * FROM record1"); //查询record表
	int count=0;
System.out.println("1111111111111111111111111111111111111111111111111111111111111111");
while(rs.next()) {//閬嶅巻鏌ヨ缁撴灉聽聽聽
    System.out.printf("%s\t",rs.getString(1));
	System.out.printf("%s\t",rs.getString(2));
	System.out.printf("%s\t",rs.getString(3));		          
	System.out.printf("%s\t",rs.getString(4));
	System.out.printf("%s\t"+"\n",rs.getString(5));
	  
	 rowData[count][0]=rs.getString(1);//使用到数组变量
		rowData[count][1]=rs.getString(2);
		rowData[count][2]=rs.getString(3);
		rowData[count][3]=rs.getString(4);
		rowData[count][4]=rs.getString(5);
		count++;
		//System.out.printf("%s\t"+"\n",rowData[count][1]);
	//System.out.println("8888888888888888888888888888888888888888888888888888888888888888888888888888888888");
}

con.close();
}
catch(SQLException e1) { 
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
}


void init() {
	
	 
	
	DefaultTableModel model = new DefaultTableModel(rowData,columnNames);//rowData,columnNames	
	 JTable table = new JTable( model);
	 JScrollPane jscrollpane=new JScrollPane(table);
	model.setDataVector(rowData,  columnNames); 
	
 	
 	
	button2 =  new JButton("退出");
	button2.addActionListener(new MyListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
		int j= JOptionPane.showConfirmDialog(null,"确定要退出吗?", "确认对话框",JOptionPane.YES_NO_OPTION);
		if (j==JOptionPane.YES_OPTION) {System.exit(0);}
		//if (j==JOptionPane.NO_OPTION) {System.exit(0);}//不退出没有改变
		}
		});

baseBox=Box.createVerticalBox();
boxV1=Box.createHorizontalBox();
boxV2=Box.createHorizontalBox();
boxV1.add(jscrollpane,BorderLayout.CENTER);
//boxV2.add(button1);
boxV2.add(button2);
baseBox.add(boxV1);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(boxV2);
add(baseBox); 
}	
public AR2 () {
	 super("显示数据库所有借阅者");   //调用父类构造函数,必须放在类方法的第一句 /*******************************************************************/
     setLayout(new java.awt.FlowLayout());
     actionP();
     init();
     setVisible(true);
     setBounds(500,500,510,560);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }


}
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页