JAVA JDBC
目录[ Hide ]
JDBC连接数据库与查询数据
import java.sql.*;
/**
* Code by pureqh on 2021-04-07
*/
public class MainTest {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //注册一个驱动
//DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root"); //建立连接 数据库路径、帐号、密码
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root");
//创建statement ,与数据库交互,一定要这个对象
Statement st = conn.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
ResultSet rs = st.executeQuery(sql);
//遍历查询每一条记录
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id +"====name="+name+"==age="+age);
}
rs.close();
st.close();
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* Code by pureqh on 2021-04-07
*/
public class MainTest {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //注册一个驱动
//DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root"); //建立连接 数据库路径、帐号、密码
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root");
//创建statement ,与数据库交互,一定要这个对象
Statement st = conn.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
ResultSet rs = st.executeQuery(sql);
//遍历查询每一条记录
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id +"====name="+name+"==age="+age);
}
rs.close();
st.close();
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
JDBC释放资源util类
package com.pure.test;
import com.pure.util.JDBCUtil;
import java.sql.*;
/**
* Code by pureqh on 2021-04-07
*/
public class MainTest {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //注册一个驱动
//Driver类中的静态代码块已经注册了驱动,所以只需要将数据库加载器类反射即可
Class.forName("com.mysql.jdbc.Driver").newInstance();
//DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root"); //建立连接 数据库路径、帐号、密码
conn = DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root");
//创建statement ,与数据库交互,一定要这个对象
st = conn.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
//遍历查询每一条记录
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id +"====name="+name+"==age="+age);
}
} catch (Exception throwables) {
throwables.printStackTrace();
} finally{
JDBCUtil.release(conn,st,rs);
}
}
}
import com.pure.util.JDBCUtil;
import java.sql.*;
/**
* Code by pureqh on 2021-04-07
*/
public class MainTest {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //注册一个驱动
//Driver类中的静态代码块已经注册了驱动,所以只需要将数据库加载器类反射即可
Class.forName("com.mysql.jdbc.Driver").newInstance();
//DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root"); //建立连接 数据库路径、帐号、密码
conn = DriverManager.getConnection("jdbc:mysql://192.168.150.131/student?" ,"root","root");
//创建statement ,与数据库交互,一定要这个对象
st = conn.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
//遍历查询每一条记录
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id +"====name="+name+"==age="+age);
}
} catch (Exception throwables) {
throwables.printStackTrace();
} finally{
JDBCUtil.release(conn,st,rs);
}
}
}
package com.pure.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
工具类优化,将配置写入工具类
package com.pure.test;
import com.pure.util.JDBCUtil;
import java.sql.*;
/**
* Code by pureqh on 2021-04-07
*/
public class MainTest {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConn();
//创建statement ,与数据库交互,一定要这个对象
st = conn.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
//遍历查询每一条记录
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id +"====name="+name+"==age="+age);
}
} catch (Exception throwables) {
throwables.printStackTrace();
} finally{
JDBCUtil.release(conn,st,rs);
}
}
}
import com.pure.util.JDBCUtil;
import java.sql.*;
/**
* Code by pureqh on 2021-04-07
*/
public class MainTest {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConn();
//创建statement ,与数据库交互,一定要这个对象
st = conn.createStatement();
//执行查询,得到结果集
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
//遍历查询每一条记录
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id +"====name="+name+"==age="+age);
}
} catch (Exception throwables) {
throwables.printStackTrace();
} finally{
JDBCUtil.release(conn,st,rs);
}
}
}
package com.pure.util;
import java.sql.*;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
static String url = "jdbc:mysql://192.168.150.131/student?";
static String name = "root";
static String password = "root";
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,name,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
import java.sql.*;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
static String url = "jdbc:mysql://192.168.150.131/student?";
static String name = "root";
static String password = "root";
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,name,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
工具类优化,将配置信息写入porperties,使用静态代码块读取,配置文件在src目录下
package com.pure.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static{
//创建属性配置对象
Properties properties = new Properties();
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//导入输入流
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//读取属性
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
}
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url,name,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static{
//创建属性配置对象
Properties properties = new Properties();
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//导入输入流
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//读取属性
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
}
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url,name,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
jdbc.properties:
url=jdbc:mysql://192.168.150.131/student
name=root
password=root
driverClass=com.mysql.jdbc.Driver
JDBC CRUD (查询 添加 junit )
project structure --> Libraries添加两个包(junit-4.12和hamcrest-core-1.3)
ctrl+shift 选择要测试的方法将代码复制到方法中。
右击run 该方法即可
package com.pure.jdbc;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 使用junit执行单元测试
* Code by pureqh on 2021-05-03
*/
public class TestDemo {
@Test
//查询
public void testQuery(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行sql语句,返回ResultSet
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
//遍历结果集
while (rs.next()){
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(name+" " +age);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.release(conn,st,rs);
}
}
//添加
@Test
public void testInsert(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
try {
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行添加语句
String sql = "insert into t_stu value(4 ,'aobama' ,59)";
int result = st.executeUpdate(sql);
if (result>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st);
}
}
}
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 使用junit执行单元测试
* Code by pureqh on 2021-05-03
*/
public class TestDemo {
@Test
//查询
public void testQuery(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行sql语句,返回ResultSet
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
//遍历结果集
while (rs.next()){
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(name+" " +age);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtil.release(conn,st,rs);
}
}
//添加
@Test
public void testInsert(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
try {
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行添加语句
String sql = "insert into t_stu value(4 ,'aobama' ,59)";
int result = st.executeUpdate(sql);
if (result>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st);
}
}
}
JDBC 删除数据
public void testDelete(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
try {
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行添加语句
String sql = "delete from t_stu where id = 4";
int result = st.executeUpdate(sql);
if (result>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st);
}
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
try {
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行添加语句
String sql = "delete from t_stu where id = 4";
int result = st.executeUpdate(sql);
if (result>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st);
}
}
JDBC 更新数据
public void testUpdate(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
try {
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行添加语句
String sql = "update t_stu set age = 99 where name ='zhangsan' ";
int result = st.executeUpdate(sql);
if (result>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st);
}
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
try {
conn = JDBCUtil.getConn();
//2.根据连接对象获取statement
st = conn.createStatement();
//3.执行添加语句
String sql = "update t_stu set age = 99 where name ='zhangsan' ";
int result = st.executeUpdate(sql);
if (result>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st);
}
JDBC DAO模式
查询接口
package com.pure.dao;
/**
* Code by pureqh on 2021-05-04
*/
public interface UserDao {
/*
* 查询
* */
void query();
}
/**
* Code by pureqh on 2021-05-04
*/
public interface UserDao {
/*
* 查询
* */
void query();
}
接口实现
package com.pure.dao.impl;
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
}
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
}
测试类
package com.pure.test;
import com.pure.dao.UserDao;
import com.pure.dao.impl.UserDaoImpl;
import org.junit.Test;
/**
* Code by pureqh on 2021-05-04
*/
public class TestUserDaoImpl {
@Test
public void testQuery(){
UserDao dao = new UserDaoImpl();
dao.query();
}
}
import com.pure.dao.UserDao;
import com.pure.dao.impl.UserDaoImpl;
import org.junit.Test;
/**
* Code by pureqh on 2021-05-04
*/
public class TestUserDaoImpl {
@Test
public void testQuery(){
UserDao dao = new UserDaoImpl();
dao.query();
}
}
工具类
package com.pure.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static{
//创建属性配置对象
Properties properties = new Properties();
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//导入输入流
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//读取属性
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
}
public static Connection getConn(){
Connection conn = null;
try {
//Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url,name,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn,Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Code by pureqh on 2021-05-03
*/
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static{
//创建属性配置对象
Properties properties = new Properties();
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//导入输入流
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//读取属性
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
}
public static Connection getConn(){
Connection conn = null;
try {
//Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url,name,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs){
closeRS(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn,Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRS(ResultSet rs){
try {
if (rs!=null){
rs.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st){
try {
if (st!=null){
st.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn){
try {
if (conn!=null){
conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn = null;
}
}
}
配置文件
jdbc.properties:
url=jdbc:mysql://192.168.150.131/user
name=root
password=root
driverClass=com.mysql.jdbc.Driver
JDBC粗糙的登陆功能
package com.pure.dao.impl;
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
@Override
public void login(String username, String password) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user where username='"+username+"' and password ='"+password+"'";
rs = st.executeQuery(sql);
if (rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
}
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
@Override
public void login(String username, String password) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user where username='"+username+"' and password ='"+password+"'";
rs = st.executeQuery(sql);
if (rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
}
package com.pure.test;
import com.pure.dao.UserDao;
import com.pure.dao.impl.UserDaoImpl;
import org.junit.Test;
/**
* Code by pureqh on 2021-05-04
*/
public class TestUserDaoImpl {
@Test
public void testQuery(){
UserDao dao = new UserDaoImpl();
dao.query();
}
@Test
public void login(){
UserDao dao = new UserDaoImpl();
dao.login("admin", "admin");
}
}
import com.pure.dao.UserDao;
import com.pure.dao.impl.UserDaoImpl;
import org.junit.Test;
/**
* Code by pureqh on 2021-05-04
*/
public class TestUserDaoImpl {
@Test
public void testQuery(){
UserDao dao = new UserDaoImpl();
dao.query();
}
@Test
public void login(){
UserDao dao = new UserDaoImpl();
dao.login("admin", "admin");
}
}
PrepareStatement(预编译)
由于Statement会产生sql注入隐患,所以使用预编译
package com.pure.dao.impl;
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.*;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
@Override
public void login(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "select * from t_user where username=? and password =?";
ps = conn.prepareStatement(sql);
//?下标从1开始
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if (rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps,rs);
}
}
}
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.*;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
@Override
public void login(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "select * from t_user where username=? and password =?";
ps = conn.prepareStatement(sql);
//?下标从1开始
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if (rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps,rs);
}
}
}
PrepareStatement增删改查
接口:
接口实现类
package com.pure.dao.impl;
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.*;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
@Override
public void login(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "select * from t_user where username=? and password =?";
ps = conn.prepareStatement(sql);
//?下标从1开始
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if (rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps,rs);
}
}
@Override
public void insert(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "insert into t_user values(null,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
int result = ps.executeUpdate();
if (result>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps);
}
}
@Override
public void delete(int id) {
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "Delete from t_user where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
int result = ps.executeUpdate();
if (result>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps);
}
}
@Override
public void update(int id, String username) {
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "update t_user set username=? where id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setInt(2,id);
int result = ps.executeUpdate();
if (result>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps);
}
}
}
import com.pure.dao.UserDao;
import com.pure.util.JDBCUtil;
import org.junit.Test;
import java.sql.*;
/**
* Code by pureqh on 2021-05-04
*/
public class UserDaoImpl implements UserDao {
@Override
public void query() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
st = conn.createStatement();
String sql = "select * from t_user";
rs = st.executeQuery(sql);
while (rs.next()){
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println(userName+" "+passWord);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,st,rs);
}
}
@Override
public void login(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "select * from t_user where username=? and password =?";
ps = conn.prepareStatement(sql);
//?下标从1开始
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if (rs.next()){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps,rs);
}
}
@Override
public void insert(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "insert into t_user values(null,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
int result = ps.executeUpdate();
if (result>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps);
}
}
@Override
public void delete(int id) {
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "Delete from t_user where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
int result = ps.executeUpdate();
if (result>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps);
}
}
@Override
public void update(int id, String username) {
Connection conn = null;
PreparedStatement ps = null;
//1.获取连接对象
conn = JDBCUtil.getConn();
//2.创建Statement对象
try {
//?内将被正确的处理为数据而非语句,无论传入什么数据都将被编译为字符串
String sql = "update t_user set username=? where id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setInt(2,id);
int result = ps.executeUpdate();
if (result>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.release(conn,ps);
}
}
}
测试类
package com.pure.test;
import com.pure.dao.UserDao;
import com.pure.dao.impl.UserDaoImpl;
import org.junit.Test;
/**
* Code by pureqh on 2021-05-04
*/
public class TestUserDaoImpl {
@Test
public void testQuery(){
UserDao dao = new UserDaoImpl();
dao.query();
}
@Test
public void login(){
UserDao dao = new UserDaoImpl();
dao.login("admin", "admin8");
}
@Test
public void insert(){
UserDao dao = new UserDaoImpl();
dao.insert("admin1", "admin8");
}
@Test
public void delete(){
UserDao dao = new UserDaoImpl();
dao.delete(1);
}
@Test
public void update(){
UserDao dao = new UserDaoImpl();
dao.update(1,"admin999");
}
}
import com.pure.dao.UserDao;
import com.pure.dao.impl.UserDaoImpl;
import org.junit.Test;
/**
* Code by pureqh on 2021-05-04
*/
public class TestUserDaoImpl {
@Test
public void testQuery(){
UserDao dao = new UserDaoImpl();
dao.query();
}
@Test
public void login(){
UserDao dao = new UserDaoImpl();
dao.login("admin", "admin8");
}
@Test
public void insert(){
UserDao dao = new UserDaoImpl();
dao.insert("admin1", "admin8");
}
@Test
public void delete(){
UserDao dao = new UserDaoImpl();
dao.delete(1);
}
@Test
public void update(){
UserDao dao = new UserDaoImpl();
dao.update(1,"admin999");
}
}