java提供的接口/api
java.sql.Driver 驱动的管理
java.sql. Connection 连接的管理
java.sql.Statement 操作数据库管理
java.sql.ResultSet 结果集
一.JDBC的基本操作
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class JDBCTest01 {
public static void main(String[] args) throws Exception{
//1.注册驱动
//DriverManager.registerDriver(new Driver());
//2.使用反射注册驱动
/*
*解决代码冗余,只注册一次驱动
*/
Class.forName("com.mysql.jdbc.Driver");
//创建连接
String url = "jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8";
/*url
* (1)jdbc:mysql://localhost:3306/day06?useUnicode=true&characterEncoding=utf-8
* (2)jdbc:mysql://[ip地址]:[端口号]/[库名]
* (3)连接我本地数据库 ; 端口号是3306
* jdbc:mysql:///库名
*
* */
Connection con = DriverManager.getConnection(url,"root","centrald");
//创建操作数据库的对象
Statement sta = con.createStatement();
/*常用的方法
* executeQuery("sql语句"):用于关键字select ,返回值是一个结果集
* executeUpdate("sql"):用于关键字 update delete insert 返回值是一个int类型的值 (受到影响的数据条数)
* execute("语句"){了解}:如果我们写select 语句 那么返回true 如果 update delete insert 返回false
* */
//操作并且返回结果
ResultSet rs = sta.executeQuery("select * from user");
//对结果集进行操作
while(rs.next()){
/* rs.getXxx(String 字段的名字);
rs.getXxx(int 索引值(从1开始)); */
int id = rs.getInt("id");
int id1 = rs.getInt(1);
String username = rs.getString("username");
String username1 = rs.getString(2);
System.out.println(id+"=="+username);
System.out.println("------------------");
System.out.println(id1+"=="+username1);
}
//关闭连接
rs.close();
sta.close();
con.close();
}
}
/*
1==tom
------------------
1==tom
2==fox
------------------
2==fox
3==james
------------------
3==james
*/
二.JDBC常用方法,使用Junit测试
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.mysql.jdbc.Driver;
/*常用的方法
* executeQuery("sql语句"):用于关键字select ,返回值是一个结果集
* executeUpdate("sql"):用于关键字 update delete insert 返回值是一个int类型的值 (受到影响的数据条数)
* execute("语句"){了解}:如果我们写select 语句 那么返回true 如果 update delete insert 返回false
* */
public class JDBCtest02 {
/*JUnit测试
* @Test
(1)方法不能用static修饰
(2)不能进行传递参数
(3)不能有返回值
*/
@Test
public void addTest() throws Exception{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接对象
String url = "jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8";
Connection con = DriverManager.getConnection(url,"root","centrald");
//创建操作数据库的对象
Statement sta = con.createStatement();
//执行sql语句
int row = sta.executeUpdate("insert into user values(null,'jaguar','qwer','jaguar@163.com')");
if(row>0){
System.out.println("ok");
}
else {
System.out.println("!ok");
}
sta.close();
con.close();
}
@Test
public void deletTest() throws Exception{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
String url = "jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8";
Connection con = DriverManager.getConnection(url,"root","centrald");
//创建操作数据库的对象
Statement sta = con.createStatement();
//执行sql
int row = sta.executeUpdate("delete from user where id=4 ");
if(row>0){
System.out.println("ok");
}
else{
System.out.println("!ok");
}
sta.close();
con.close();
}
@Test
public void updateTest() throws Exception{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接对象
String url = "jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8";
Connection con = DriverManager.getConnection(url,"root","centrald");
//创建操作数据库的对象
Statement sta = con.createStatement();
//执行语句
int row = sta.executeUpdate("update user set password='fox2' where id=3");
if(row>0){
System.out.println("ok!");
}
else{
System.out.println("!ok");
}
sta.close();
con.close();
}
@Test
public void selectByid() throws Exception{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
String url = "jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8";
Connection con = DriverManager.getConnection(url, "root", "centrald");
//创建操作数据库的对象
Statement sta = con.createStatement();
//执行语句返回结果
ResultSet rs = sta.executeQuery("select * from user where id = 1");
if(rs.next()){
int id = rs.getInt(1);
String username = rs.getString(2);
System.out.println(id+"==="+username);
System.out.println("ok!");
}
else{
System.out.println("!ok");
}
rs.close();
sta.close();
con.close();
}
}
三.解决SQL注入问题,使用PrepareStatement
jdbc.properties
2.JDBCTest03.java
从properties文件中读取配置信息
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javax.resource.cci.ResultSet;
public class JDBCTest03 {
private static final String DRIVERNAME;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
static{
ResourceBundle bun = ResourceBundle.getBundle("jdbc");
DRIVERNAME = bun.getString("DRIVERNAME");
URL = bun.getString("URL");
USERNAME = bun.getString("USERNAME");
PASSWORD = bun.getString("PASSWORD");
}
static{
try {
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
//创建连接对象
Connection con = DriverManager.getConnection(URL,USERNAME, PASSWORD);
return con;
}
public static void closeResource(ResultSet rs,Statement sta,Connection con){
try{
if(rs!=null){
rs.close();
}
if(sta!=null){
sta.close();
}
if(con!=null){
con.close();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.JDBCTest04.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
import com.mysql.jdbc.JDBC4UpdatableResultSet;
public class JDBCTest04 {
@Test
public void addTest() throws Exception{
//获得连接对象
Connection con = JDBCTest03.getConnection();
//获得操作数据库的对象
String sql = "insert into user values (null,?,?,?)";
PreparedStatement sta = con.prepareStatement(sql);
sta.setString(1, "lisa");
sta.setString(2, "lili2");
sta.setString(3, "lisa@163.com");
int row = sta.executeUpdate();
if(row>0){
System.out.println("ok");
}
else{
System.out.println("!ok");
}
JDBCTest03.closeResource(null, sta, con);
}
@Test
public void deleteTest() throws Exception{
//获得连接对象
Connection con = JDBCTest03.getConnection();
//获得操作对象
String sql = "delete from user where id=?";
PreparedStatement sta = con.prepareStatement(sql);
//赋值
sta.setInt(1, 11);
int row = sta.executeUpdate();
if(row>0){
System.out.println("ok!");
}
else{
System.out.println("!ok");
}
JDBCTest03.closeResource(null, sta, con);
}
@Test
public void updateTest() throws Exception{
//获得连接对象
Connection con = JDBCTest03.getConnection();
//获得操作对象
String sql = "update user set password = ? where id =?";
PreparedStatement sta = con.prepareStatement(sql);
//赋值
sta.setString(1, "12345");
sta.setInt(2, 8);
int row = sta.executeUpdate();
if(row>0){
System.out.println("ok!");
}
else{
System.out.println("!ok");
}
JDBCTest03.closeResource(null, sta, con);
}
@Test
public void selectById() throws Exception{
//获得连接对象
Connection con = JDBCTest03.getConnection();
//创建操作的对象
String sql = "select * from user where id = ?";
PreparedStatement sta = con.prepareStatement(sql);
//赋值
sta.setInt(1, 1);
ResultSet rs = sta.executeQuery();
if(rs.next()){
System.out.println("ok!");
}
else{
System.out.println("!ok");
}
rs.close();
JDBCTest03.closeResource(null, sta, con);
}
}