test當然是資料庫名, 給你一個例子: 驅動程式名String driver = "com.mysql.jdbc.Driver"; // URL指向要訪問的資料庫名scutcsString url = "jdbc:mysql://127.0.0.1:3306/scutcs"; // MySQL配置時的使用者名稱String user = "root";// Java連線MySQL配置時的密碼 String password = "root"; try {// 載入驅動程式 Class.forName(driver); // 連續資料庫Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed())System.out.println("Succeeded connecting to the Database!"); // statement用來執行SQL語句Statement statement = conn.createStatement(); // 要執行的SQL語句String sql = "select * from student"; 結果集ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("執行結果如下所示:"); System.out.println("-----------------"); System.out.println(" 學號" + "\t" + " 姓名"); System.out.println("-----------------"); String name = null; while(rs.next()) { 選擇sname這列資料name = rs.getString("sname");// 首先使用ISO-8859-1字符集將name解碼為位元組序列並將結果儲存新的位元組陣列中。 // 然後使用GB2312字符集解碼指定的位元組陣列 name = new String(name.getBytes("ISO-8859-1"),"GB2312"); // 輸出結果System.out.println(rs.getString("sno") + "\t" + name); } rs.close(); conn.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }
test當然是資料庫名, 給你一個例子: 驅動程式名String driver = "com.mysql.jdbc.Driver"; // URL指向要訪問的資料庫名scutcsString url = "jdbc:mysql://127.0.0.1:3306/scutcs"; // MySQL配置時的使用者名稱String user = "root";// Java連線MySQL配置時的密碼 String password = "root"; try {// 載入驅動程式 Class.forName(driver); // 連續資料庫Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed())System.out.println("Succeeded connecting to the Database!"); // statement用來執行SQL語句Statement statement = conn.createStatement(); // 要執行的SQL語句String sql = "select * from student"; 結果集ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("執行結果如下所示:"); System.out.println("-----------------"); System.out.println(" 學號" + "\t" + " 姓名"); System.out.println("-----------------"); String name = null; while(rs.next()) { 選擇sname這列資料name = rs.getString("sname");// 首先使用ISO-8859-1字符集將name解碼為位元組序列並將結果儲存新的位元組陣列中。 // 然後使用GB2312字符集解碼指定的位元組陣列 name = new String(name.getBytes("ISO-8859-1"),"GB2312"); // 輸出結果System.out.println(rs.getString("sno") + "\t" + name); } rs.close(); conn.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }