importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;public classMain {public static voidQuery() {
Connection connection= null;try{
String SDdriver= "com.mysql.jdbc.Driver";
String SDurl= "jdbc:mysql://localhost:3306/emsdb?characterEncoding=utf8&useSSL=true";
//emsdb替换成自己的数据库名称
String SDuser= "root";
String SDpassword= "123456";
Class.forName(SDdriver);
connection=DriverManager.getConnection(SDurl, SDuser, SDpassword);
}catch(Exception e) {
e.printStackTrace();
}
PreparedStatement preparedStatement= null;
ResultSet resultSet= null;
String ss= new String("SELECT * from employer;");try{
preparedStatement=connection.prepareStatement(ss);
resultSet=preparedStatement.executeQuery();while(resultSet.next()) {
System.out.println(resultSet.getString("ename"));
}
}catch(SQLException ex) {
}finally{
closeAll(connection, preparedStatement,resultSet);
}
}public static voidcloseAll(Connection connection, PreparedStatement preparedStatement,ResultSet resultSet) {try{
//按顺序关闭if(resultSet!=null) {
resultSet.close();
}if (preparedStatement!=null) {
preparedStatement.close();
}if (connection != null) {
connection.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}public static voidmain(String[] args) {//TODO Auto-generated method stub
Query();
}
}