Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn =DriverManager.
getConnection("jdbc:mysql://localhost/mydata?" "user=root&password=heiheijoshua");
stmt = conn.createStatement();
rs = stmt.executeQuery("select *from dept");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex){
System.out.println("SQLException: " +ex.getMessage());
System.out.println("SQLState: " +ex.getSQLState());
System.out.println("VendorError: " +ex.getErrorCode());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException sqlEx){
sqlEx.printStackTrace();
}
}
其实说实话,真正现在的大多数的数据操作都已经被框架封装好了,比如说JDBC的操作,已经被java的指令,方法,annotation之类的代替了,所以关于MySQL和JDBC的操作,掌握其原理是主要的,其实操作都很简单。