public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//试图建立到给定数据库 URL 的连接。DriverManager 试图从已注册的 JDBC 驱动程序集中选择一个适当的驱动程序。
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
//创建一个 Statement 对象来将 SQL 语句发送到数据库。
stmt = conn.createStatement();
//执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。
rs = stmt.executeQuery("select * from dept");
//将光标从当前位置向前移一行
while(rs.next()){
System.out.println(rs.getString("deptno"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
conn = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(rs != null) {
rs.close();
rs = null;
}
} catch(Exception e) {
}
}
}
浙公网安备 33010602011771号