这主要是为了自己备忘,所以很粗糙。
public static void main(String[] args) throws Exception {
// 加载MySQL的JDBC的驱动
Class.forName("com.mysql.jdbc.Driver");
Connection con = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "123");
statement = con.prepareStatement("select * from user");
rs = statement.executeQuery();
while(rs.next()){
System.out.println(rs.getObject(2));
}
} catch (Exception e) {
if(rs != null) rs.close();
if(statement != null) statement.close();
if(con != null) con.close();
e.printStackTrace();
}
}