java 关于数据库的连接并查询(二)
程序员文章站
2024-03-15 18:19:24
...
编写一个应用程序,连接SQL Server数据库Stu,并访问数据库中grade表,并可以通过课程号下拉表列选择课程号,并将表中的内容输出在窗口界面的表格中
public class Grade {
public static void main(String args[]){
String tableHead[];
String content[][];
JTable table ;
JComboBox com=new JComboBox();
com.addItem("all");
com.addItem("C01");
com.addItem("C02");
com.addItem("C03");
com.addItem("C04");
com.addItem("C05");
com.addItem("C06");
JFrame win = new JFrame();
Query findRecord = new Query();
findRecord.setDatabaseName("STU20200101");
findRecord.setSQL("select * from GradeS");
content = findRecord.getRecord();
tableHead=findRecord.getColumnName();
table = new JTable(content,tableHead);
DefaultTableModel model = new DefaultTableModel(content,tableHead);
table.setModel(model);
TableRowSorter sorter = new TableRowSorter(model);
table.setRowSorter(sorter);
com.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String s = com.getSelectedItem().toString();
if(s=="all") {
sorter.setRowFilter(RowFilter.regexFilter("."));
}
else if(s=="C01") {
sorter.setRowFilter(RowFilter.regexFilter("C01"));
}
else if(s=="C02") {
sorter.setRowFilter(RowFilter.regexFilter("C02"));
}
else if(s=="C03") {
sorter.setRowFilter(RowFilter.regexFilter("C03"));
}
else if(s=="C04") {
sorter.setRowFilter(RowFilter.regexFilter("C04"));
}
else if(s=="CO5") {
sorter.setRowFilter(RowFilter.regexFilter("C05"));
}
else if(s=="C06") {
sorter.setRowFilter(RowFilter.regexFilter("C06"));
}
}
});
win.add(com,BorderLayout.NORTH);
win.add(new JScrollPane(table),BorderLayout.CENTER);
win.setBounds(200,200,500,400);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setTitle("数据库顺序查询");
}
}
public class Query {
String databaseName=""; //数据库名
String SQL; //SQL语句
String columnName[]; //全部字段(列)名
String record[][]; //查询到的记录
public Query() {
try{ Class.forName("com.mysql.jdbc.Driver"); //加载JDBC-MySQL驱动
}
catch(Exception e){}
}
public void setDatabaseName(String s) {
databaseName=s.trim();
}
public void setSQL(String SQL) {
this.SQL=SQL.trim();
}
public String[] getColumnName() {
if(columnName ==null ){
System.out.println("先查询记录");
return null;
}
return columnName;
}
public String[][] getRecord() {
startQuery();
return record;
}
private void startQuery() {
Connection con;
Statement sql;
ResultSet rs;
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=STU20200101";
try {
con=DriverManager.getConnection(url,"sa","123456");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery(SQL);
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount(); //字段数目
columnName=new String[columnCount];
for(int i=1;i<=columnCount;i++){
columnName[i-1]=metaData.getColumnName(i);
}
rs.last();
int recordAmount =rs.getRow(); //结果集中的记录数目
record = new String[recordAmount][columnCount];
int i=0;
rs.beforeFirst();
while(rs.next()) {
for(int j=1;j<=columnCount;j++){
record[i][j-1]=rs.getString(j); //第i条记录,放入二维数组的第i行
}
i++;
}
con.close();
}
catch(SQLException e) {
System.out.println("请输入正确的表名"+e);
}
}
}
运行结果:
上一篇: Kotlin入门与进阶:基础知识(二)类、对象、数组、区间
下一篇: MVP框架