package bd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTest { public static void main(String[] args) { // 驅動程式名 String driver = "com.mysql.jdbc.Driver"; // URL指向要訪問的資料庫名 String url = "jdbc:mysql://192.168.238.10:3306/F4base"; // MySQL配置時的使用者名稱 String user = ""; // MySQL配置時的密碼 String password = ""; 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(); for(int i=0;i<10;i++) { String insertSql = "insert into Test(cmd,name,time) values("+i+","+(2*i)+",20170402)"; // statement.executeUpdate(insertSql); } // 要執行的SQL語句 String sql = "select * from Test"; // 結果集 ResultSet rs = statement.executeQuery(sql); // String name = null; while (rs.next()) { System.out.println(rs.getString("cmd") + " " + rs.getString("name") + " "+ rs.getString("time")); } 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(); } } }