public static void JdbcClient(String url,String account,String password ){
Connection con=null;
PreparedStatement prepardstatement=null;
try {
//创建连接
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection(url,account,password);
//操作数据库
prepardstatement=con.prepareStatement("select now() from dual");
ResultSet resultSet=prepardstatement.executeQuery();
while (resultSet.next()){
System.out.println("根据下标获取:"+resultSet.getDate(1));
System.out.println("根据别名获取:"+resultSet.getDate("now()"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (con!=null){
con.close();
}
System.out.println("Connection是否关闭:"+con.isClosed());
} catch (SQLException e) {
e.printStackTrace();
}
}
// 最终输出结果:
// 根据下标获取:2018-03-19
// 根据别名获取:2018-03-19
// Connection是否关闭:true
}