简介
- Java DataBase Connectivity Java数据库连接
- JDBC是一套标准,是Java与各大数据库厂商共同定制的一套接口. 这套接口由各大数据库厂商进行了实现.
使用
1. 引入jar文件.
2. 加载数据库驱动 (JavaSE项目中可以省略 , JavaWeb项目必须编写此步骤)
Class.forName("com.mysql.jdbc.Driver");
3. 通过驱动管理器, 获取JDBC连接对象.
Connection conn = DriverManager.getConnection("数据库连接地址","帐号","密码");
4. 通过连接对象, 创建SQL执行对象 (SQL执行环境)
Statement state = conn.createStatement();
5. 通过SQL执行对象 ,执行SQL语句.
state.execute(String sql语句);
6. 释放资源
state.close();
conn.close();
package com.java.demo;
import java.sql.*;
public class Demo1 {
public static void createTable() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123");
Statement state = conn.createStatement();
state.execute("create table person(id int,nickname varchar(32))");
state.close();
conn.close();
}
public static void insertData() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "root", "123");
Statement state = conn.createStatement();
state.execute("insert into person values(1,'张三')");
state.close();
conn.close();
}
public static void insertPerson() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
Statement state = conn.createStatement();
int flag = state.executeUpdate("insert into person values(1,'张三'),(2,'李四'),(3,'王二麻子')");
state.close();
conn.close();
System.out.println("返回:"+flag);
}
public static void deletePerson() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
Statement state = conn.createStatement();
int flag = state.executeUpdate("delete from person where id=1");
state.close();
conn.close();
System.out.println("返回:"+flag);
}
public static void updatePerson() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
Statement state = conn.createStatement();
int flag = state.executeUpdate("update person set nickname='haha' where id=2");
state.close();
conn.close();
System.out.println("返回:"+flag);
}
public static void selectPerson() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery("select * from person");
while(rs.next()){
int id = rs.getInt("id");
String nickname = rs.getString("nickname");
System.out.println("id="+id+"\tnickname="+nickname);
}
rs.close();
state.close();
conn.close();
}
}
PreparedStatement 预编译的SQL执行环境
内部实现原理:
1. 将未拼接参数的SQL语句, 作为SQL指令, 先传递给数据库 进行编译.
2. 再将参数传递给数据库, 此时传递的参数不会再作为指令执行, 只会被当作文本存在.
操作流程与Statement基本一致:
1. 如何得到一个PreparedStatement 对象
PreparedStatement state = conn.prepareStatement("预编译的SQL语句");
2. 预编译的SQL语句如何编写
需要填充参数的位置, 使用?代替即可! 例如:
select id from xzk_user where username=? and password=?
3. 参数如何填充
state.setXXX(int index,XXX value);
setXXX中XXX指的是数据类型,
参数1: index : SQL语句中?的索引值 , 从1开始
参数2: value : 填充的参数值.
4. 如何执行填充完毕参数的SQL
- boolean execute();
- int executeUpdate();
- ResultSet executeQuery();
@Override
public boolean findByPassword(String username, String password) {
Connection conn = null;
PreparedStatement state = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
state = conn.prepareStatement("select * from xzk_user where username=? and password=?");
state.setString(1,username);
state.setString(2,password);
rs = state.executeQuery();
return rs.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
rs.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
state.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
conn.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
return false;
}