JDBC
- 导入驱动jar包,注册驱动,获取连接
- 定义sql
- 获取执行sql的对象,执行sql
- 处理结果:查询需要处理结果,增删改只需要判断count>0,有没有执行成功;
- 释放资源
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url,username,password);
String sql = "";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
resultSet.close();
statement.close();
connection.close();
List<User> list = new ArrayList();
while(resultSet.next()){
int UserId = resultSet.getString("userId");
User user = new User(UserId);
list.add(user);
}
String sql = "insert into tb(name,age) values(?,?)";
String sql = "update tb set name = ?,age = ? where id = ?";
String sql = "delete from tb where id = ?";
statement.setString(1,name);
statement.setInt(1,age);
int count = statement.executeUpdate(sql);
System.out.println(count > 0);
标签:JDBC,String,resultSet,statement,sql,close,tb,复习
From: https://www.cnblogs.com/lm02/p/18098594