jdbc连接数据库操作

首先要创建一个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();
}
}
}


-------------------------------------------

然后就是创建一个表结构的类

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.v.dao;


import java.util.List;


import cn.entity.Student;


public interface DeptDao {
public int update(Student stu);
public int inset(Student stu);
public int delete(Student stu);
//全查
public List<Student> selectAll();
//根据id查信息
public List<Student> selectById(int id);


}


创建一个数据库操作的实例化一个对象,并实现操作接口

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;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值