JDBC操作
导入jdbc依赖包
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
public class Jdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection connection = DriverManager.getConnection(url,username,password);
//创建数据库操作对象statement
Statement statement = connection.createStatement();
//要执行的sql语句
String sql = "select * from users ";
//执行sql语句,返回结果集
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("age"));
System.out.println(resultSet.getObject("name"));
System.out.println(resultSet.getObject("address"));
}
//关闭连接
resultSet.close();
statement.close();
connection.close();
}
}
预编译,防止sql注入
public class TestJdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection connection = DriverManager.getConnection(url, username, password);
//编写sql语句
String sql = "insert into users(id, name, age, address) values(?,?,?,?)";
//预编译
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,4);
preparedStatement.setString(2,"唐三");
preparedStatement.setInt(3,20);
preparedStatement.setString(4,"武汉");
//执行sql
int i = preparedStatement.executeUpdate();
if (i>0){
System.out.println("插入成功");
}
//关闭连接,释放资源
preparedStatement.close();
connection.close();
}
}
jdbc关于事务
事务具有原子性,要么都成功执行,要么都不成功执行
单元测试的包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
public class TestJdbcS {
//单元测试,需要导入junit包
@Test
public void hello(){
System.out.println("hello");
}
@Test
public void test(){
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123";
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
//开启事务
connection.setAutoCommit(false);
String sql1 = "update account set money=money-100 where `'user'`='a'";
connection.prepareStatement(sql1).executeUpdate();
//制造错误
int i =1/0;
String sql2 = "update account set money=money+100 where `'user'`='b'";
connection.prepareStatement(sql2).executeUpdate();
//提交事务
connection.commit();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
//出现异常就回滚事务
try {
connection.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}finally {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}