第一部分:
第二部分:
package com.pangdudu.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.junit.Test;
public class JDBCTools {
// 编写测试类,测试数据库是否连接
@Test
public void test1() {
Connection con = getConnection();
if (con != null) {
System.out.println("ok");
} else {
System.out.println("error");
}
}
// 创建数据库的连接
public static Connection getConnection() {
// 获取Connection对象,连接数据库
Connection connection = null;
// 获取Properties的值
Properties properties = new Properties();
// 获取输入流,从而来获取连接数据库的一些属性的值
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
try {
// 注册数据库(将属性的值放到properties中)
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
// 获取数据库连接的四个对象
String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
try {
// 注册数据库
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
// 通过DriverManager的getConnection()的方法来获取连接数据库所需的位置、用户名和密码
connection = DriverManager.getConnection(jdbcUrl, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// 关闭连接操作
public static void release(Connection connection, Statement statement,
ResultSet resultset) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (resultset != null) {
resultset.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 编写测试类,测试method()方法是否是成功的
@Test
public void test2() {
// 插入操作的操作
// String sql1 =
// "INSERT INTO teacher(flag,tnumber,tname,tpassword,sex,tbirthday ) "
// + "VALUES(0,10,'陌陌桑',123,'man','1995-09-10')";
// method(sql1);
// 删除语句的操作
// String sql2 = "DELETE FROM teacher WHERE flag=0 AND tnumber=10";
// method(sql2);
// 修改语句的操作
String sql3 = "UPDATE teacher SET flag=1 " + "WHERE tnumber=10";
method(sql3);
}
// 编写方法method(),可以实现insert、delete和update,除了select
public static void method(String sql) {
// 获取需要的Connection和Statement对象
Connection connection = getConnection();
Statement statement = null;
try {
// 调用Connection的createStatement()方法获取statement
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
try {
// 调用Statement的executeUpdate()方法,将sql语句的内容添加到数据库中
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
release(connection, statement, null);
}
}
// 编写方法selcet方法,实现数据的查找
public static List<Object> select(String sql) {
// 创建list集合,将数据装在其中
List<Object> list = new ArrayList<>();
// 获取Connection、Statement和ResultSet的对象
Connection connection = null;
Statement statement = null;
ResultSet resultset = null;
try {// 获取数据库的连接
connection = getConnection();
// 调用Connection的createStatement()方法
statement = connection.createStatement();
// 调用statement的executeQuery()方法,获取数据库的主要内容
resultset = statement.executeQuery(sql);
//
if (resultset.next()) {
// 这里写的是主要要查询的具体数据
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// PreparedStatement调用的实现(主要实现的是insert和update)
public static void testPreparedStatement(String sql) {
// 获取Connection和PreparedStatement对象
Connection connection = null;
PreparedStatement preparedstatement = null;
try {
// 连接数据库
connection = getConnection();
// 调用connection的preparedStatement(sql)方法
preparedstatement = connection.prepareStatement(sql);
// 给数据库的数据进行赋值(setXxx()方法)
preparedstatement.setInt(1, 0);
// 在这里要实现Date(数据类型(日期))的使用
// preparedstatement.setDate(int index,
// new Date(new java.util.Date().getTime()));
//执行preparedStatement的executeUpdate()方法,更新数据库资源
preparedstatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
release(connection, preparedstatement, null);
}
}
//这里写的是一个update()方法,使用的是PreparedStatement式
//@param sql
//@param args: 填写SQL占位符的可变参数
public static void update(String sql , Object ...args ){
//获取Connection和PreparedStatement
Connection connection=null;
PreparedStatement preparedstatement=null;
try{
//获取数据库的连接
connection=getConnection();
//调用Connection的preparedStatement(sql)方法
preparedstatement=connection.prepareStatement(sql);
for(int i=0; i<args.length;i++){
preparedstatement.setObject(i+1, args[i]);
}
//调用preparedStatement的executeUpdate()方法更新数据库
preparedstatement.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
release(connection, preparedstatement, null);
}
}
}
第二部分:
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
user=root
password=q1245.....
第三部分:
使用PreparedStatement
1)使用Statement需要进行拼写SQL语句,容易出错
2)使用PreparedStatement
①创建PreparedStatement
String sql="INSERT INTO xxx VALUES(?,?,?,?,?,?)"
PreparedStatement preparedstatement=con.prepareStatement(sql);
②调用PreparedStatement的setXxx(int index, Object val)设置占位符 的值(索引值index从1开始)
③执行SQL语句:exectueQuery(查询)或executeUpadate()(更新)
执行时,不再需要传入SQL语句
3)PreparedStatement:是Statement的子接口,
可以传入带占位符的SQL语句,并且提供了补充占位符变量的方法