jdbc
1.添加依赖
2.创建jdbc.properties
3.创建工具类进行封装
4.使用 PreparedStatement
5.完成增删改查项目
一 、添加依赖
加入junit依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
加入test依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
二 、创建jdbc.properties
找到resources 进行创建 创建名为:properties
写入:
driver = com.mysql.cj.jdbc.Driver 表示 添加驱动意思
url = jdbc:mysql://localhost:3306/2207b 表示创建数据库连接意思
username = root 表示用户名意思
password = 123456 表示密码意思
三、创建工具类进行封装
封装工具类代码
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
//静态
static {
InputStream is = null;
try {
//读取 jdbc.properties 内容
is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//创建 Properties 对象 进行使用
Properties properties = new Properties();
properties.load(is);
//从jdbc.properties 拿到数据赋值给相对的值 进行使用
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 给is抛出去判断is里的内容是否为空 如果为空进行 throw new RuntimeException(e)
// 如果is里不为空进行关闭资源
// 关闭资源的close意思
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
//进行封装Connection
public static Connection getConnection() throws ClassNotFoundException, SQLException {
//创建驱动
Class.forName(driver);
//创建 数据库 连接
Connection connection = DriverManager.getConnection(url, username, password);
//进行返回connection进行执行
return connection;
}
//进行封装clo关闭资源
public static void clo(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
//判断resultSet 查询不为空 进行关闭资源
if (resultSet != null) {
resultSet.close();
}
//判断statement 语句对像不为空 进行关闭资源
if (statement != null) {
statement.close();
}
//判断connection 数据库连接不为空 进行关闭资源
if (connection != null) {
connection.close();
}
}
}
四、使用 PreparedStatement
PreparedStatement 占位符对象
PreparedStatement statement = connection.prepareStatement(sql语句);
statement.修改类型(表示占位第几个,内容);
使用案例:
String users_password_sql = "select * from upss where users=? and password=?";
//使用工具类进行调用并使用
Connection connection = Jdbcut.getConnection();
// PreparedStatement 表示占位对象 使用? 进行占位
PreparedStatement statement = connection.prepareStatement(users_password_sql);
statement.setString(1, users);
statement.setString(2, password);
//执行sql查询使用
ResultSet resultSet = statement.executeQuery();
五、完成增删改查项目
案例一
添加
public void addtext() throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("id:");
int id = scanner.nextInt();
System.out.println("name:");
String name = scanner.next();
System.out.println("price:");
int price = scanner.nextInt();
System.out.println("shu:");
int shu = scanner.nextInt();
//sql语句
String addsql = "insert into shang (id,name,price,shu) values (?,?,?,?)";
//使用工具类进行调用并使用
Connection connection = JDBCUtils.getConnection();
// PreparedStatement 表示占位对象 使用? 进行占位
PreparedStatement statement = connection.prepareStatement(addsql);
statement.setInt(1, id);
statement.setString(2, name);
statement.setInt(3, price);
statement.setInt(4, shu);
//执行sql添加使用;
statement.executeUpdate();
//进行释放资源
JDBCUtils.clo(null, statement, connection);
}
案例二
删除
public static void shan() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("id:");
int id = scanner.nextInt();
//sql语句
String shansql = "delete from shang where id = ?";
//使用工具类进行调用并使用
Connection connection = JDBCUtils.getConnection();
// PreparedStatement 表示占位对象 使用? 进行占位
PreparedStatement statement = connection.prepareStatement(shansql);
statement.setInt(1, id);
//执行sql删使用
statement.executeUpdate();
//进行释放资源
JDBCUtils.clo(null, statement, connection);
}
案例三
改
public static void gai() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("name:");
String name = scanner.next();
System.out.println("price:");
int price = scanner.nextInt();
System.out.println("shu:");
int shu = scanner.nextInt();
System.out.println("id:");
int id = scanner.nextInt();
//sql语句
String gaisql = "update shang set name = ? ,price = ?, shu = ? where id = ? ";
//使用工具类进行调用并使用
Connection connection = JDBCUtils.getConnection();
// PreparedStatement 表示占位对象 使用? 进行占位
PreparedStatement statement = connection.prepareStatement(gaisql);
statement.setString(1, name);
statement.setInt(2, price);
statement.setInt(3, shu);
statement.setInt(4, id);
//执行sql改使用
statement.executeUpdate();
//进行释放资源
JDBCUtils.clo(null, statement, connection);
}