*jdbc
先创建5包,分别为:
com.bdqn.dao(BaseDao.java DeptDao.java);
com.bdqn.dao.impl(DeptImp.java);
com.bdqn.entity(Dept.java);
com.bdqn.text(Text.java);测试包
com.bdqn.untils(Contants.java)
package com.bdqn.dao;
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.Properties;
import com.bdqn.utils.Contants;
public class BaseDao {
private static Connection conn = null;
/*private static String driver; // 数据库驱动字符串
private static String url; // 连接URL字符串
private static String user; // 数据库用户名
private static String password; // 用户密码
*/
static{ //在项目启动加载
System.out.println("哈哈哈");
init();
}
//初始化配置文件
public static void init(){
try {
Properties properties = new Properties();
//得到文件的路径
String fileName = "database.properties";
//文件加载到流里面
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(fileName);
//读取配置信息
properties.load(is);
Contants.driver= properties.getProperty("driver");
Contants.url= properties.getProperty("url");
Contants.user= properties.getProperty("user");
Contants.pwd= properties.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection(){
//加载驱动
try {
Class.forName(Contants.driver);
conn = DriverManager.getConnection(Contants.url, Contants.user, Contants.pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭资源
public static void close(Connection conn ,Statement st, ResultSet rs){
try {
if(rs !=null){
rs.close();
}
if(st !=null){
st.close();
}
if(conn !=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//增删改的方法
public static int update(String sql,Object[] objects){
int num=0;
PreparedStatement ps = null;
try {
conn = getConnection();
ps =conn.prepareStatement(sql);
if(objects !=null && objects.length>0){
for (int i = 0; i < objects.length; i++) {
ps.setObject((i+1),objects[i]);
}
}
num= ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(conn, ps, null);
}
return num;
}
//查询
public static ResultSet getResultSet(String sql,Object[] objects){
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
if(objects !=null && objects.length>0){
for (int i = 0; i < objects.length; i++) {
ps.setObject((i+1), objects[i]);
}
}
rs= ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
接口为以下:
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Dept;
public interface DeptDao {
//增加
int insertDept(Dept dept);
//修改
int updateDept(Dept dept);
//删除
int deleteDept(int did);
//全查
List<Dept> selectAllDept();
//根据id查询
List<Dept> selectDept(int did);
}
实现方法:
package com.bdqn.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.bdqn.dao.BaseDao;
import com.bdqn.dao.DeptDao;
import com.bdqn.entity.Dept;
public class DeptImpl extends BaseDao implements DeptDao {
@Override
public int insertDept(Dept dept) {
String sql = "insert into dept values(?,?,?,?)";
/*Dept dept2 = new Dept();*/
Object [] objects = {dept.getDid(),dept.getDname(),dept.getDage(),dept.getDmesc()};
int num = this.update(sql, objects);
return num;
}
@Override
public int updateDept(Dept dept) {
String sql = "update dept set dname=? where did=?";
/*Dept dept2 = new Dept();*/
Object [] objects = {dept.getDname(),dept.getDid()};
int num = this.update(sql, objects);
return num;
}
@Override
public int deleteDept(int did) {
String sql = "delete from dept where did=?";
/*Dept dept2 = new Dept();*/
Object [] objects = {did};
int num = this.update(sql, objects);
return num;
}
@Override
public List<Dept> selectAllDept() {
String sql = "select * from dept";
List<Dept> list =null;
try {
ResultSet rs = this.getResultSet(sql, null);
list = new ArrayList<Dept>();
while (rs.next()) {
Dept dept = new Dept();
int did = rs.getInt("did");
String dname = rs.getString("dname");
int dage = rs.getInt("dage");
String dmesc = rs.getString("dmesc");
dept.setDid(did);
dept.setDname(dname);
dept.setDage(dage);
dept.setDmesc(dmesc);
list.add(dept);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public List<Dept> selectDept(int did) {
String sql = "select * from dept where did=?";
List<Dept> list =null;
Object[] objects={did};
try {
ResultSet rs = this.getResultSet(sql, objects);
list = new ArrayList<Dept>();
while (rs.next()) {
Dept dept = new Dept();
int did1 = rs.getInt("did");
String dname = rs.getString("dname");
int dage = rs.getInt("dage");
String dmesc = rs.getString("dmesc");
dept.setDid(did1);
dept.setDname(dname);
dept.setDage(dage);
dept.setDmesc(dmesc);
list.add(dept);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
在实体类进行封装
package com.bdqn.entity;
import java.io.Serializable;
public class Dept implements Serializable{
private int did;
private String dname;
private int dage;
private String dmesc;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getDage() {
return dage;
}
public void setDage(int dage) {
this.dage = dage;
}
public String getDmesc() {
return dmesc;
}
public void setDmesc(String dmesc) {
this.dmesc = dmesc;
}
public Dept(int did, String dname, int dage, String dmesc) {
this.did = did;
this.dname = dname;
this.dage = dage;
this.dmesc = dmesc;
}
public Dept() {
}
@Override
public String toString() {
return "Dept [did=" + did + ", dname=" + dname + ", dage=" + dage
+ ", dmesc=" + dmesc + "]";
}
}
服务类中存储配置信息
package com.bdqn.utils;
public class Contants {
public static String driver;
public static String url;
public static String user;
public static String pwd;
}
最后在测试类中测试
package com.bdqn.text;
import java.util.List;
import com.bdqn.dao.DeptDao;
import com.bdqn.dao.impl.DeptImpl;
import com.bdqn.entity.Dept;
public class Text {
public static void main(String[] args) {
DeptDao dao = new DeptImpl();
/* Dept dept = new Dept();
dept.setDname("aaa");
dept.setDid(10);*/
List<Dept> list = dao.selectDept(2);
for(Dept dept : list){
System.out.println(dept.toString());
}
}
}
总结:对于jdbc链接数据库,思路大致明白,可是操作不一定能不看打出来,必须要多练,对于增删改查还有不熟悉的地方。