ORM:
DBUtils:
public static Connection getConnenction(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=school","sa","123");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(Connection connection , Statement statement , ResultSet resultSet){
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement !=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Entity:
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<User> userList = new ArrayList<>();
connection = DBUtils2.getConnection();
preparedStatement=connection.prepareStatement("select * from [user]");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String id = resultSet.getString("id");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
User user = new User();
user.setId(id);
user.setUsername(username);