JDBC 操作数据库
jdbc:
package org.java.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 里面编写了所有的关于数据库操作的方法
* 具有通用性的增删改查方法
*/
public class BaseDao {
//驱动字符串
private String className = "com.mysql.jdbc.Driver";
//连接字符串
private String url = "jdbc:mysql://localhost:3306/syspet";
//用户名
private String user = "root";
//密码
private String password = "root";
/*jdbc接口*/
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
/**
* 创建连接
* @return 连接对象
*/
public Connection getConnection(){
try {
//加载mysql驱动
Class.forName(className);
//通过DriverManager来管理mysql驱动,创建与数据库的连接
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 增删改的方法
* @param sql 预编译的sql(有问号的占位符)
* @param params 因为参数的类型不确定所以用Object,个数不确定所以用的数组
*/
public void update(String sql,Object[] params){
//1.创建连接
conn = this.getConnection();
try {
//2.创建预编译声明对象
ps = conn.prepareStatement(sql);
//将参数数组放入到预编译的sql语句中
if(params != null){
//3.表示有参数,遍历数组
for(int i = 0; i < params.length; i++){
//4.将参数数组放入到预编译sql语句中
ps.setObject(i + 1, params[i]);
}
}
//5.执行sql语句
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//操作完成后关闭资源
this.close(conn, ps, rs);
}
}
/**
* 查询
* @param sql 预编译的sql语句
* @param params 参数数组
* @return 结果集
*/
public ResultSet getResultSet(String sql,Object[] params){
//1.创建连接
conn = this.getConnection();
try {
//2.创建预编译声明对象,准备做查询
ps = conn.prepareStatement(sql);
//判断是否有参数
if(params != null){
//3.遍历参数数组
for(int i = 0; i < params.length; i++){
ps.setObject(i + 1, params[i]);
}
}
//4.执行查询得到结果集
rs = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//查询的时候现在还不能关闭(这个时候一定不能关闭资源)
return rs;
}
/**
*关闭资源
*/
public void close(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if(rs != null){
rs.close();
rs = null;
}
if(ps != null){
ps.close();
ps = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
实体类:
package org.java.entity;
/**
* 实体层(对数据库中实体对象的封装)
*/
public class Bank {
private Integer id; //编号
private String name; //姓名
private Integer blance; //余额
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBlance() {
return blance;
}
public void setBlance(Integer blance) {
this.blance = blance;
}
}
dao层:
package org.java.dao;
import java.util.List;
import org.java.entity.Bank;
/**
* 接口中的所有方法都默认是用public abstract修饰
*/
public interface BankDao {
/**
* 往数据库中添加一个对象
* @param bank bank对象
*/
void add(Bank bank);
/**
* 修改一个bank对象
* @param bank
*/
public abstract void update(Bank bank);
/**
* 删除
* @param id
*/
public void delete(Integer id);
/**
* 查询所有的
* @return
*/
abstract List<Bank> getAllBank();
}
dao实现层:
package org.java.dao.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.java.dao.BankDao;
import org.java.dao.BaseDao;
import org.java.entity.Bank;
public class BankDaoImpl extends BaseDao implements BankDao {
@Override
public void add(Bank bank) {
String sql = "INSERT INTO bank(NAME,blance) VALUES(?,?)";
Object[] params = {bank.getName(),bank.getBlance()};
super.executeUpdate(sql, params);
}
@Override
public void update(Bank bank) {
String sql = "UPDATE bank SET blance=? WHERE NAME=?";
Object[] params = {bank.getBlance(),bank.getName()};
super.executeUpdate(sql, params);
}
@Override
public void delete(Integer id) {
String sql = "DELETE FROM bank WHERE id=?";
Object[] params = {id};
super.executeUpdate(sql, params);
}
@Override
public List<Bank> getAllBank() {
List<Bank> list = new ArrayList<Bank>();
String sql = "SELECT * FROM bank";
Object[] params = null;
super.rs = super.getResultSet(sql, params);
//遍历结果集
try {
while(rs.next()){
//将结果集封装到bank对象中
Bank bank = new Bank();
bank.setId(rs.getInt(1));
bank.setName(rs.getString(2));
bank.setBlance(rs.getInt(3));
//将对象放入到集合中
list.add(bank);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库资源
super.close(conn, ps, rs);
}
return list;
}
}
。。。service层暂时忽略
测试:
package org.java.test;
import java.util.List;
import java.util.Scanner;
import org.java.dao.BankDao;
import org.java.dao.impl.BankDaoImpl;
import org.java.entity.Bank;
import org.junit.Test;
public class BankTest {
BankDao bankDao = new BankDaoImpl();
@Test
public void testAdd(){
Scanner input = new Scanner(System.in);
//创建要添加的bank对象
Bank bank = new Bank();
System.out.println("请输入账户姓名:");
bank.setName(input.next());
System.out.println("请输入账户余额:");
bank.setBlance(input.nextInt());
//添加到数据库中
bankDao.add(bank);
}
@Test
public void testupdate(){
Scanner input = new Scanner(System.in);
//创建要添加的bank对象
Bank bank = new Bank();
System.out.println("请输入账户姓名:");
bank.setName(input.next());
System.out.println("请输入账户余额:");
bank.setBlance(input.nextInt());
//添加到数据库中
bankDao.update(bank);
}
@Test
public void testselect(){
List<Bank> list = bankDao.getAllBank();
for (Bank bank : list) {
System.out.println(bank.getId() + "\t" + bank.getName() + "\t" + bank.getBlance());
}
}
}