jdbc数据库操作(二)单例模式
来源:互联网 发布:mac虚拟机玩天龙八部 编辑:程序博客网 时间:2024/06/06 15:41
创建单例模式,用来加载驱动
//要点:
两个私有的属性
一个私有方法
一个对外开放的静态方法,返回当前链接
package cn.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import com.sun.org.apache.regexp.internal.recompile;
public class ConfigManage {
private static ConfigManage configManage;
private static Properties properties;
private ConfigManage() {
properties = new Properties();
InputStream is = null;
try {
is = ConfigManage.class.getClassLoader().getResourceAsStream(
"database.properties");
properties.load(is);
} catch (InvalidPropertiesFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static synchronized ConfigManage getInstance() {
if (configManage == null) {
synchronized (ConfigManage.class) {
if (configManage == null) {
configManage = new ConfigManage();
}
}
}
return configManage;
}
public String getValues(String key) {
return (String) properties.get(key);
}
}
创建一个BaseDao类,他用来创建一些数据库操作方法:
package cn.v.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.untils.Untils;
public class BaseDao {
public static Connection conn = null;
// 创建连接
public static Connection connction() {
try {
Class.forName(Untils.name);
conn = DriverManager.getConnection(Untils.url, Untils.user,
Untils.pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 增删改
public static int update(String sql, Object[] ob) {
PreparedStatement ps = null;
Connection con = connction();
int count = 0;
try {
ps = con.prepareStatement(sql);
for (int i = 0; i < ob.length; i++) {
ps.setObject(i + 1, ob[i]);
}
count = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getClose(con, ps, null);
return count;
}
// 查询
public static ResultSet select(String sql, Object[] ob) {
Connection con = connction();
ResultSet rs = null;
try {
PreparedStatement ps = con.prepareStatement(sql);
if (ob != null && ob.length > 0) {
for (int i = 0; i < ob.length; i++) {
ps.setObject(i + 1, ob[i]);
}
}
rs = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
// 模糊查询
public static ResultSet selectLike(String sql, String like) {
Connection con = connction();
ResultSet rs = null;
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
ps.setString(1, like);
rs = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
// 关闭连接
public static void getClose(Connection conn, PreparedStatement ps,
ResultSet rs) {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
----------------------------------------------
创建一个StudentDao的接口并定义抽象方法
package cn.dao;
import java.util.List;
import cn.entity.Student;
public interface StudentDao {
//根据学生编号修改学生爱好
public int update(int id,String hobby);
//插入学生信息
public int inset(Student stu);
//根据学生编号删除学生信息
public int delete(int id);
//全查
public List<Student> selectAll();
//根据班级id查信息
public List<Student> selectById(int gid);
//根据id查信息
public List<Student> selectByLike(String like);
}
-------------------------------------------
然后就是创建一个表结构的类
Student 类
package cn.entity;
public class Student {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
private String desc;
public Student(int id, String name, int age, String desc) {
this.id = id;
this.name = name;
this.age = age;
this.desc = desc;
}
public Student() {
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", desc=" + desc + "]";
}
}
创建一个数据库操作的实例化一个对象,并实现操作接口
package cn.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.entity.Student;
import cn.v.dao.BaseDao;
import cn.v.dao.DeptDao;
public class StuImpl extends BaseDao implements DeptDao {
// 根据id更改学生简介
public int update(Student stu) {
String sql = "update dept set desc=? where did=? ";
Object[] ob = { stu.getDesc(), stu.getId() };
int i = this.update(sql, ob);
return i;
}
// 插入学生信息
public int inset(Student stu) {
String sql = "insert into dept values(?,?,?,?)";
Object[] ob = { stu.getId(), stu.getName(), stu.getAge(), stu.getDesc() };
int i = this.update(sql, ob);
return i;
}
// 根据id删除学生信息
public int delete(Student stu) {
String sql = "delete from dept where did=? ";
Object[] ob = { stu.getId() };
int i = this.update(sql, ob);
return i;
}
// 全查
public List<Student> selectAll() {
String sql = "select *from dept ";
List<Student> list = new ArrayList<Student>();
try {
ResultSet rs = this.select(sql, null);
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("did"));
stu.setName(rs.getString("dname"));
stu.setAge(rs.getInt("dage"));
stu.setDesc(rs.getString("desc"));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// 根据id查信息
public List<Student> selectById(int id) {
String sql = "select *from dept where did=?";
Object[] ob = { id };
List<Student> list = new ArrayList<Student>();
try {
ResultSet rs = this.select(sql, ob);
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("did"));
stu.setName(rs.getString("dname"));
stu.setAge(rs.getInt("dage"));
stu.setDesc(rs.getString("desc"));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// 根据id查信息
public List<Student> selectLike(String like) {
String sql = "select * from dept where dname like '%?%' ? ";
List<Student> list = new ArrayList<Student>();
try {
ResultSet rs = this.selectLike(sql, like);
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("did"));
stu.setName(rs.getString("dname"));
stu.setAge(rs.getInt("dage"));
stu.setDesc(rs.getString("desc"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}