14、JDBC
什么是JDBC:Java数据库连接
需要jar包支持:
- java.sql
- javax.sql
- mysql-connector-java.jar 连接驱动(必须要导入)
实验环境搭建
手写sql:【预备知识】
CREATE TABLE users (
id INT PRIMARY KEY,
`name` VARCHAR(40),
`password` VARCHAR(40),
email VARCHAR(60),
birthday DATE
);
INSERT INTO users(id,`name`,`password`,email,birthday) VALUES(1,'工一','123456','gongyi@qq.com','2000-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday) VALUES(2,'工一2','1234562','gongyi2@qq.com','2001-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday) VALUES(3,'工一3','1234563','gongyi3@qq.com','2002-01-01');
SELECT * FROM users;
导入数据库依赖
<!--mysql的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
idea中连接数据库
JDBC固定步骤:
1.加载驱动
2.连接数据库,代表数据库
3.向数据库发送SQL的对象Statement:CRUD
4.编写SQL(根据业务,不同的SQL)
5.执行SQL
6.关闭连接
代码show
TestJdbc.java
package com.gongyi.test;
import java.sql.*;
public class TestJdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
//useUnicode=true&characterEncoding=utf-8 解决中文乱码
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";//可以从ida的数据库连接那里粘贴过来
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,4);//给第一个占位符?的值赋值为4
preparedStatement.setString(2,"工一说Java");//给第二个占位符?的值赋值为工一说Java
preparedStatement.setString(3,"123456");//给第三个占位符?的值赋值为123456
preparedStatement.setString(4,"gysayjava@qq.com");//给第四个占位符?的值赋值为gysayjava@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();
}
}
TestJdbc2.java【预编译】
package com.gongyi.test;
import java.sql.*;
public class TestJdbc2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
//useUnicode=true&characterEncoding=utf-8 解决中文乱码
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,PreparedStatement:CRUD
Statement statement = connection.createStatement();
//4.编写SQL
String sql = "select * from users";
/*String sql = "delete from users where id =4";
//受影响的行数,增删改都使用:executeUpdate
int i = statement.executeUpdate(sql);*/
//5.执行查询SQL,返回一个ResultSet:结果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
System.out.println("id="+rs.getObject("id"));
System.out.println("password="+rs.getObject("password"));
System.out.println("name="+rs.getObject("name"));
System.out.println("email="+rs.getObject("email"));
System.out.println("birthday="+rs.getObject("birthday"));
}
//6.关闭连接,释放资源(一定要做) 先开后关
rs.close();
statement.close();
connection.close();
}
}
彩蛋
1.架构思想
没有什么是加一层解决不了的
例如:数据库驱动等
2.idea中连接数据库
1)idea右侧无database选项怎么办?
去view中勾选
3.idea的数据库编写sql提示快捷键
4.java连接数据库url可以从idea database连接那里拷贝过来
拷贝内容:
jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
拷贝位置:
5.SQL注入问题