JDBC编写流程
什么是jdbc:java连接数据库!
需要jar包的支持:
- java.sql
- javax.sql
- mysql-connection-java...(连接驱动)
数据库依赖创建
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhang</groupId>
<artifactId>javaweb-jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
</project>
jdbc固定步骤:
- 加载驱动
- 连接数据库
- 创建Statement
- 编写sql
- 执行sql
- 关闭连接
不安全的数据库连接
package com.zhang.until;
import java.sql.*;
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="6113081";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.链接数据库,其对象就代表了数据库(不安全)
Connection connection = DriverManager.getConnection(url, username, password);
//向数据库发送sql的对象:用它来做crud操作statement(普通),preparedStatement(安全)但其需要预编译
Statement statement = connection.createStatement();
//编写sql
//String sql="select * from users";
String sql="delete from users where id=4";
//预编译开启安全的数据库连接
//PreparedStatement preparedStatement = connection.prepareStatement(sql);
//执行查询sql并返回了一个结果集
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println("id="+resultSet.getObject("id"));
System.out.println("name="+resultSet.getObject("name"));
System.out.println("password="+resultSet.getObject("password"));
System.out.println("email="+resultSet.getObject("email"));
System.out.println("birthday="+resultSet.getObject("birthday"));
}
//关闭连接释放资源(一定要做)先开的后关
resultSet.close();
statement.close();
connection.close();
}
}
安全的数据库连接(预编译版本)
package com.zhang.until;
import java.sql.*;
public class JDBCTest02 {
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="6113081";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.链接数据库,其对象就代表了数据库(不安全)
Connection connection = DriverManager.getConnection(url, username, password);
//预编译开启安全的数据库连接
String sql="insert into users(id, name, password, email, birthday) VALUES (?,?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,6);
preparedStatement.setString(2,"falltime张三");
preparedStatement.setString(3,"123456");
preparedStatement.setString(4,"falltime@qq.com");
preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
//执行查询sql
int i = preparedStatement.executeUpdate();
if (i > 0) {
System.out.println("成功插入数据");
}
//关闭连接释放资源(一定要做)先开的后关
preparedStatement.close();
connection.close();
}
}
本文来自博客园,作者:Cn_FallTime,转载请注明原文链接:https://www.cnblogs.com/CnFallTime/p/15981434.html