html css js jquery
msyql oracle sql server
jdbc: java database connection
1. 设计思路数据库有很多种:mysql,oracle,sql server…
我们使用jdbc操作数据库,难道每一种都要去学习吗?
怎么做到以最小的成本学习操作所有的数据库。 想到了学习开车!!?
因为所有的车,你发现结构和功能都是一样的。
2. java提供的接口/类 2.1 DriverManagerDriverManager类: 获取数据库连接
//驱动管理器
public class DriverManager {
@CallerSensitive
public static Connection getConnection(String url,
String user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();
if (user != null) {
info.put("user", user);
}
if (password != null) {
info.put("password", password);
}
return (getConnection(url, info, Reflection.getCallerClass()));
}
}
2.2 Connection
Connection数据库连接接口:
public interface Connection extends Wrapper, AutoCloseable {
// 创建语句块(把sql发射到数据库)
Statement createStatement() throws SQLException;
Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException;
// 创建语句块(把sql发射到数据库),可以sql注入
PreparedStatement prepareStatement(String sql) throws SQLException;
//关闭连接
void close() throws SQLException;
}
2.3 Statement
Statement接口:
public interface Statement extends Wrapper, AutoCloseable {
//执行查询sql
ResultSet executeQuery(String sql) throws SQLException;
//执行修改sql 新增,修改,删除
int executeUpdate(String sql) throws SQLException;
//关闭
void close() throws SQLException;
}
2.4 PreparedStatement
PreparedStatement接口:
public interface PreparedStatement extends Statement {
//执行查询sql,没有sql参数,因为在创建的时候,就已经把sql传进去了
ResultSet executeQuery() throws SQLException;
//执行修改sql,没有sql参数
int executeUpdate() throws SQLException;
//为占位符赋值 ?
void setBoolean(int parameterIndex, boolean x) throws SQLException;
void setByte(int parameterIndex, byte x) throws SQLException;
void setInt(int parameterIndex, int x) throws SQLException;
void setDouble(int parameterIndex, double x) throws SQLException;
void setString(int parameterIndex, String x) throws SQLException;
void close() throws SQLException;
}
2.5 ResultSet
ResultSet接口:查询的结果集合
public interface ResultSet extends Wrapper, AutoCloseable {
//判断查询的结果下面还没有一条数据
boolean next() throws SQLException;
void close() throws SQLException;
//获取一条数据中列的值
String getString(int columnIndex) throws SQLException;
boolean getBoolean(int columnIndex) throws SQLException;
int getInt(int columnIndex) throws SQLException;
double getDouble(int columnIndex) throws SQLException;
int getInt(String columnLabel) throws SQLException;
double getDouble(String columnLabel) throws SQLException;
String getString(String columnLabel) throws SQLException;
java.sql.Date getDate(String columnLabel) throws SQLException;
}
3. jdbc的开发流程
哪个数据库想要使用java去操作该数据库,那么该数据库的厂商就实现这些接口
-
把mysql驱动包(jar包-实现了jdbc一系列接口的java项目)引入到项目
-
在项目路径下,创建文件夹lib
-
把驱动包拷贝到该文件夹中
-
右击jar – build path – add to build path
-
-
创建连接
-
编写sql
-
创建语句块
-
执行sql
-
关闭资源
package com.qf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//import com.mysql.jdbc.Driver;
public class Demo01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1. 加载驱动(类,加载mysql的使用说明)
Class.forName("com.mysql.jdbc.Driver");
//2. 创建连接
String url = "jdbc:mysql://localhost:3307/qf_hefei";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
//com.mysql.jdbc.JDBC4Connection
System.out.println(connection);
//3. 编写sql 操作数据库指令(sql)
String sql = "INSERT INTO teacher VALUES(NULL, '普海')";
//4. 创建语句块 sql发射器
Statement stm = connection.createStatement();
//5. 执行sql
stm.executeUpdate(sql);
//6. 关闭资源
stm.close();
connection.close();
}
}
3.2 执行查询sql
package com.qf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1. 加载驱动(类,加载mysql的使用说明)
Class.forName("com.mysql.jdbc.Driver");
//2. 创建连接
String url = "jdbc:mysql://localhost:3307/hefei";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
//3. 编写sql 操作数据库指令(sql)
String sql = "SELECT * FROM student";
//4. 创建语句块 sql发射器
Statement stm = connection.createStatement();
//5. 执行sql
ResultSet rs = stm.executeQuery(sql);
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String code = rs.getString("code");
String gender = rs.getString("gender");
int classId = rs.getInt("class_id");
System.out.println(id +"t"+ name+"t"+code+"t"+gender+"t"+classId);
}
//7. 关闭资源
stm.close();
connection.close();
}
}
4. 封装dao
思路:把对数据库的操作定义在一个方法中
类名: 表名映射类名+Dao
package com.qf.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TeacherDao {
public void addTeacher(String name) throws ClassNotFoundException, SQLException {
//1. 加载驱动(类,加载mysql的使用说明)
Class.forName("com.mysql.jdbc.Driver");
//2. 创建连接
String url = "jdbc:mysql://localhost:3307/hefei";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
//3. 编写sql 操作数据库指令(sql)
String sql = "INSERT INTO teacher VALUES(NULL, '"+name+"')";
//4. 创建语句块 sql发射器
Statement stm = connection.createStatement();
//5. 执行sql 添加
stm.executeUpdate(sql);
//6. 关闭资源
stm.close();
connection.close();
}
}
package com.qf.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.qf.entity.Student;
public class StudentDao {
public void addStudent(String name, String code, String gender, int classId) throws ClassNotFoundException, SQLException {
//1. 加载驱动(类,加载mysql的使用说明)
Class.forName("com.mysql.jdbc.Driver");
//2. 创建连接
String url = "jdbc:mysql://localhost:3307/hefei";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
//3. 编写sql 操作数据库指令(sql)
//INSERT INTO student VALUES(NULL, 'xxx', 'yyy', '1', 2);
String sql = "INSERT INTO student VALUES(NULL, '"+name+"', '"+code+"', '"+gender+"', "+classId+")";
//4. 创建语句块 sql发射器
Statement stm = connection.createStatement();
//5. 执行sql 添加
stm.executeUpdate(sql);
//6. 关闭资源
stm.close();
connection.close();
}
}
5. 封装实体类
为了封装参数,可以为每个表定义一个类,这种类叫做实体类
student表 — Studetn类
package com.qf.entity;
public class Student {
private Integer id;
private String name;
private String code;
private String gender;
private Integer classId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getClassId() {
return classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
}
}
public void addStudent2(Student stu) throws ClassNotFoundException, SQLException {
//1. 加载驱动(类,加载mysql的使用说明)
Class.forName("com.mysql.jdbc.Driver");
//2. 创建连接
String url = "jdbc:mysql://localhost:3307/hefei";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
//3. 编写sql 操作数据库指令(sql)
//INSERT INTO student VALUES(NULL, 'xxx', 'yyy', '1', 2);
String sql = "INSERT INTO student VALUES(NULL, '"+stu.getName()+"', '"+stu.getCode()+"', '"+stu.getGender()+"', "+stu.getClassId()+")";
//4. 创建语句块 sql发射器
Statement stm = connection.createStatement();
//5. 执行sql 添加
stm.executeUpdate(sql);
//6. 关闭资源
stm.close();
connection.close();
}
6. 关闭资源
需要把关闭资源代码,放到finally代码中
public void addStudent3(Student stu) {
Connection connection = null;
Statement stm = null;
try {
//1. 加载驱动(类,加载mysql的使用说明)
Class.forName("com.mysql.jdbc.Driver");
//2. 创建连接
String url = "jdbc:mysql://localhost:3307/hefei";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url, user, password);
//3. 编写sql 操作数据库指令(sql)
//INSERT INTO student VALUES(NULL, 'xxx', 'yyy', '1', 2);
String sql = "INSERT INTO student VALUES(NULL, '"+stu.getName()+"', '"+stu.getCode()+"', '"+stu.getGender()+"', "+stu.getClassId()+")";
//4. 创建语句块 sql发射器
stm = connection.createStatement();
//5. 执行sql 添加
stm.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//6. 关闭资源
try {
stm.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}



