JDBC的使用
项目结构
pojo包中的Person类
package com.hxp.www.pojo;
public class Person {
private int id;
private String name;
private String pwd;
public Person() {
}
public Person(int id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String toString() {
return "Person [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
util包中的BaseDao类 工具类
package com.hxp.www.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class BaseDao {
//创建jdbc要用到的类属性
public Connection con; //连接对象
public PreparedStatement pstmt; //预编译通道
public ResultSet rs; //查询信息的集合
//1.加载驱动 建立连接 创建一个预编译通道
public Connection looding() throws Exception{
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("SQLConfig.properties");//获取本项目src/下的文件 并返回一个 InputStream对象
Properties prop = new Properties(); //创建一个
prop.load(is);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String pwd = prop.getProperty("pwd");
// Class.forName("com.mysql.cj.jdbc.Driver");
// con = DriverManager.getConnection("jdbc:mysql://localhost:3306/girls?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL","root","root");
Class.forName(driver); //加载驱动
con = DriverManager.getConnection(url,user,pwd); //创建连接
return con; //返回一个连接
}
//创建一个通用的查询方法 传递一条String sql语句 和一个Object[]数组, 返回一个查询结果的集合
//object 里面装了 sql许多的 ?变量 。 目的:防止sql注入( 就是 ("select * from admin where name=? and pwd=?",new Object[]{name,pwd}) )
public ResultSet query(String sql,Object[] object) throws Exception{
con = this.looding(); //调用加载方法 进行加载驱动和建立连接
pstmt = con.prepareStatement(sql); //创建一个预编译通道 对象
if(object==null){ //判断一下 object 是否为空 如果为空直接执行sql语句
rs = pstmt.executeQuery();
return rs;
}else{ //如果不为空 循环给所有的 ‘?’赋值(‘?’的下标是从 1 开始的)
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
rs = pstmt.executeQuery(); //最后执行executeQuery()方法 执行sql语句
return rs; //返回 查询信息的集合
}
}
//以下方法原理相同
//创建一个通用的增、删、改的方法 传递一条String sql语句 和一个Object[] 数组, 返回受影响行数
public int update(String sql,Object[] object) throws Exception{
con = this.looding();
pstmt = con.prepareStatement(sql);
if(object==null||object.equals("")){
int count = pstmt.executeUpdate();
return count;
}else{
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
int count = pstmt.executeUpdate();
return count;
}
}
//关闭资源方法 使用完记得关闭 不然会占用资源
//注意: 从内向外 关闭
public void close() throws Exception{
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
}
}
Dao包中的PersonDao类
package com.hxp.www.Dao;
import java.util.ArrayList;
import java.util.List;
import com.hxp.www.pojo.Person;
import com.hxp.www.util.BaseDao;
public class PersonDao extends BaseDao{ //继承BaseDao 可以使用他的所有属性和方法哦!
List<Person> persons = new ArrayList<Person>(); //创建一个Person类型的List集合
//查询admin表中所有的信息 返回 List<Person> 的集合
public List<Person> qbquery() throws Exception{
String sql = "select * from admin"; //简单的一句 sql语句
rs = this.query(sql, null); //调用query方法 执行查询语句 返回查询到的信息集合rs
while(rs.next()){
int id=rs.getInt("id");
String name = rs.getString("username");
String pwd = rs.getString("password"); //遍历集合添加到List<Person>集合中
persons.add(new Person(id,name,pwd));
}
this.close(); //关闭资源
return persons; //返回 List<Person>集合
}
//查询admin表中所有的信息 返回 List<Person> 的集合
public List<Person> wquery() throws Exception{
String sql = "select * from admin where id=? or id=? ";
rs = this.query(sql,new Object[]{1,45});
while(rs.next()){
int id=rs.getInt("id");
String name = rs.getString("username");
String pwd = rs.getString("password");
persons.add(new Person(id,name,pwd));
}
this.close();
return persons;
}
//根据id=46删除一条信息 反回受影响的行数
public int deletePerson() throws Exception{
String sql = "delete from admin where id=?";
int i =this.update(sql, new Object[]{46});
this.close();
return i;
}
}
test包中进行测试
package com.hxp.www.test;
import java.util.List;
import com.hxp.www.Dao.PersonDao;
import com.hxp.www.pojo.Person;
public class test2 {
public static void main(String[] args) {
try {
PersonDao dao = new PersonDao();
//查询全部admin表达的信息
List<Person> list = dao.qbquery();
for(Person p :list){
System.out.println(p);
}
//删除id=46的一条信息
// int i =dao.deletePerson();
// if(i>0){
// System.out.println("删除成功");
// }
// else{
// System.out.println("操作失败");
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}