标签:word float rgs root derby return .com i++ use
JDBC
查询操作
课上测试的几个提交点都是以上述语句为核心,Example11_3为模板
(数据库导入图片已经提交。。就没再放那个图)
第一题
import java.sql.*; import java.sql.*; public class Example11_2 { public static void main(String args[]) { Connection con; Statement sql; ResultSet rs; con = GetDBConnection.connectDB("students","root",""); if(con == null ) return; String sqlStr="select * from mess order by birthday"; try { sql=con.createStatement rs = sql.executeQuery(sqlStr); while(rs.next()) { String number=rs.getString(1); String name=rs.getString(2); Date date=rs.getDate(3); float height=rs.getFloat(4); System.out.printlf("%s\t",number); System.out.printlf("%s\t",name); System.out.printlf("%s\t",date); System.out.printlf("%.2f\n",height); } con.close(); } catch(SQLException e) { System.out.println(e); } } }
GetDBConnection 类
import java.sql.*; public class GetDBConnection { public static Connection connectDB(String DBName,String id,String p) { Connection con = null; String uri = "jdbc:mysql://localhost:3306/"+DBName+"?useSSL=true&characterEncoding=utf-8"; try{ Class.forName("com.mysql.jdbc.Driver");//加载JDBC-MySQL驱动 } catch(Exception e){} try{ con = DriverManager.getConnection(uri,id,p); //连接代码 } catch(SQLException e){} return con; } }
第二题
import javax.swing.*; public class BianCheng2 { public static void main(String args[]) { String [] tableHead; String [][] content; JTable table; JFrame win= new JFrame(); Query findRecord=new Query; findRecord.setDatabaseName(args[0]); findRecord.setSQL("select * from "+args[1]); content = findRecord.getRecord(); tableHead=findRecord.getColumnName(); table = new JTable(content,tableHead); win.add(new JScrollPane(table)); win.setBounds(12,100,400,200); win.setVisible(true); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Query类
import java.sql.*; 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 uri = "jdbc:mysql://localhost:3306/"+ databaseName+"?useSSL=true&characterEncoding=utf-8"; try { con=DriverManager.getConnection(uri,"root",""); 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); } } }
编程题第二题提示:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0,怎么调试都无法编译
解决:一个字母一个字母,一句一句扣几遍,逻辑、顺序上都没什么问题,,才想到是要带命令行参数并运行...然后在这里操作一下子就ok了
标签:word float rgs root derby return .com i++ use
原文地址:https://www.cnblogs.com/FenixRen/p/8909137.html