1.加载驱动
2.连接数据库,代表数据库
3.向数据库发送SQL的对象Statement: CRUD
4.编写SQL(根据业务,写不同的SQL)
5.执行SQL
6.关闭连接
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 = "123456";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//3.向数据库发送SQL的对象Statement,PrepareStatement(安全预编译):CRUD
Statement statement = connection.createStatement();
//4.编写SQL
String sql = "select * from users";
//String sql = "select from users where id = 1";
//i是影响到了几行,增删改都使用executeUpdate()
//int i = statement.executeUpdate(sql);
//5.执行查询SQL,返回一个结果集
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
System.out.println("id=" + rs.getObject("id"));
System.out.println("name=" + rs.getObject("name"));
System.out.println("password=" + rs.getObject("password"));
System.out.println("email=" + rs.getObject("email"));
System.out.println("birthday=" + rs.getObject("birthday"));
}
//6.关闭连接,释放资源(一定要做) 先开后关
rs.close();
statement.close();
connection.close();
}
}
预编译SQL
public class TestJdbc2 {
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 = "123456";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//3.编写SQL
String sql = "insert into users (id, name, password, email, birthday) values (?,?,?,?,?);";
//4.预编译
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 3); //给第一个占位符?的值为1
preparedStatement.setString(2, "阳"); //给第二个占位符?的值为阳
preparedStatement.setString(3, "123"); //给第三个占位符?的值为123
preparedStatement.setString(4, "yang@qq.com"); //给第四个占位符?的值为yang@qq.com
preparedStatement.setDate(5, new Date(new java.util.Date().getTime())); //给第五个占位符?的值为new Date(new java.util.Date().getTime())
//5.执行SQL
int i = preparedStatement.executeUpdate();
if (i > 0) {
System.out.println("插入成功");
}
//6.关闭连接,释放资源(一定要做) 先开后关
preparedStatement.close();
connection.close();
}
}
要么都成功,要么都失败!
ACID原则:是数据库事务正常执行的四个,分别指原子(Atomicity)、一致性(Consistency)、一致性、独立性(Isolation)及持久性(Durability),保证数据的安全。
开启事务
事务提交 commit()
事务回滚 rollBack()
关闭事务
转账:
A:1000
B:1000
A(900) --100--> B(1100) 如果服务器崩了,这100就没了,所以要避免 保证AB在一组事务中发生!!
package com.hitenine.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
//操作数据库的公共类
public class BaseDao {
private static String driver;
private static String url;
private static String username;
private static String password;
//静态代码块,类加载的时候就初始化了
static {
Properties properties = new Properties();
//通过类加载器读取对应的资源
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
driver = properties.getProperty("url");
driver = properties.getProperty("username");
driver = properties.getProperty("password");
}
//获取数据库的连接
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
//编写查询公共方法
public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
//预编译的sql,在后面直接执行就可以了
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//setObject,占据符从1开始,但是我们的数组是从0开始的
preparedStatement.setObject(i + 1, params[i]);
}
resultSet = preparedStatement.executeQuery();
return resultSet;
}
//编写增删改公共方法
public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException {
//预编译的sql,在后面直接执行就可以了
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//setObject,占据符从1开始,但是我们的数组是从0开始的
preparedStatement.setObject(i + 1, params[i]);
}
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
//释放资源
public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
boolean flag = true;
if (null != resultSet) {
try {
resultSet.close();
//GC回收
resultSet = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (null != preparedStatement) {
try {
preparedStatement.close();
//GC回收
preparedStatement = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (null != connection) {
try {
connection.close();
//GC回收
connection = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
return flag;
}
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
原文:https://www.cnblogs.com/hitenine/p/12178287.html