// 驅動程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的數據庫名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
// MySQL配置時的用戶名
String user = "root";
// 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);
String name = null;
while(rs.next()) {
System.out.println(rs.getString("sno"));
}
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();
}