java中JDBC链接oracle数据库,封装增删改查,单条查询、多条查询(终极版)

通过模拟Atm取款,测试代码

Sql中的表为:

username password balance
     
     

 

1、数据库查询包,实现了包装增删改查,多条查询,其中三个方法可以实现增、删、改、单条查询、多条查询

package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import AtmoPo.Rowmapper;
import AtmoPo.RowmapperAll;
//接口,为实现多条查询做准备
public class JdbcTemplament {
	//数据库的链接
	public Connection getConn() {
		Connection conn=null;
		try {
			//加载驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动加载失败!");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("链接数据库失败!");
			e.printStackTrace();
		}
		return conn;
	}
	//update()可以实现增删改,注意数组中的元素要与sql中一致;
	public void update (String sql,Object[] values) {
		Connection conn=getConn();
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement(sql);
			for(int i=0;i queryAll(String sql,Object[] values,RowmapperAll ra) {
		Connection conn=getConn();
		PreparedStatement ps=null;
		List list=new ArrayList();
		try {
			ps=conn.prepareStatement(sql);
			for(int i=0;i 
  

2.连个接口,单条查询与多条查询依赖的两个接口、实现单挑或者多条信息的映射

package AtmoPo;

import java.sql.ResultSet;

public interface Rowmapper {
		//此处根据需求,将rs查询的结果集用对象接收;
		 public Object rowMapper(ResultSet rs);
}
package AtmoPo;

import java.sql.ResultSet;
import java.util.List;

public interface RowmapperAll {
	public List rowMapperAll(ResultSet rs);
}
 
  

3、Atm映射

package AtmoPo;

public class Atm {
	private String username;
	private String password;
	private int balance;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}
	@Override
	public String toString() {
		return "Atm [username=" + username + ", password=" + password + ", balance=" + balance + "]";
	}
}

4.数据链接层,实现以上两个接口

package ATMDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import AtmoPo.Atm;
import AtmoPo.Rowmapper;
import AtmoPo.RowmapperAll;
import util.JdbcTemplament;

public class AtmDao implements Rowmapper,RowmapperAll {
	//存款
	public void cunkuanDao(String username,int money) {
		JdbcTemplament jtl=new JdbcTemplament();
		Atm atm=load(username);
		atm.setBalance(atm.getBalance()+money);
		String sql1="update tb_atm set balance=? where username=?";
		jtl.update(sql1, new Object[] {atm.getBalance(),atm.getUsername()});
	}
	//取款
	public void quqianDao(String username,int money) {
		JdbcTemplament jtl=new JdbcTemplament();
			Atm atm=load(username);
			if(atm.getBalance()>money) {
				atm.setBalance(atm.getBalance()-money);
				String sql1="update tb_atm set balance=? where username=?";
				jtl.update(sql1, new Object[] {atm.getBalance(),atm.getUsername()});
		}else {
				System.out.println("余额不足!");
			}
	}
	//转账
	public void zhuanzhangDao(String username,int money,String rusername) {
		JdbcTemplament jtl=new JdbcTemplament();
			Atm atm=load(username);
			if(atm.getBalance()>money) {
				atm.setBalance(atm.getBalance()-money);
				String sql1="update tb_atm set balance=? where username=?";
				jtl.update(sql1, new Object[] {atm.getBalance(),atm.getUsername()});
				//对转入账户的操作
				Atm atm1=load(rusername);
				atm1.setBalance(atm1.getBalance()+money);
				String sql3="update tb_atm set balance=? where username=?";
				jtl.update(sql3, new Object[] {atm1.getBalance(),atm1.getUsername()});
				
		}else {
				System.out.println("余额不足!");
			}
	}
	//查询余额
	public int chaxunyue(String username) {
		Atm atm=load(username);
		return atm.getBalance();
	}
	
	//封装单条查询
	public Atm  load(String username) {
		JdbcTemplament jtl=new JdbcTemplament();
		String sql="select * from tb_atm where username=?";
		Atm atm=(Atm)jtl.load(sql, new Object[] {username}, this);
		return atm;
	}
	//多条查询
	public List queryAllDao(int start,int end){
		JdbcTemplament jtl=new JdbcTemplament();
		String sql="Select * from(select rownum r ,tb_atm.* from tb_atm)t where t.r between ? and ?";
		List list=jtl.queryAll(sql, new Object[] {start,end}, this);
		return list;
	}
	@Override
	public Object rowMapper(ResultSet rs) {
		Atm atm=null;
		try {
			if(rs.next()) {
				atm=new Atm();
				atm.setUsername(rs.getString("username"));
				atm.setPassword(rs.getString("pasword"));
				atm.setBalance(rs.getInt("balance"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return atm;
	}
	@Override
	public List rowMapperAll(ResultSet rs) {
		List list= new ArrayList();
		Atm atm=null;
		try {
			while(rs.next()) {
				atm=new Atm();
				atm.setUsername(rs.getString("username"));
				atm.setPassword(rs.getString("pasword"));
				atm.setBalance(rs.getInt("balance"));
				list.add(atm);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}
 
  

5.业务层

package Service;
import ATMDao.AtmDao;
import AtmoPo.Atm;

public class AtmService {
	public void cunkuan(String username,int money) {
		AtmDao ad=new AtmDao();
		ad.cunkuanDao(username, money);
	}
	public void zhuanzhang(String username,int money,String rusername) {
		AtmDao ad=new AtmDao();
		ad.zhuanzhangDao(username, money, rusername);
	}
	public void qukuan(String username,int money) {
		AtmDao ad=new AtmDao();
		ad.quqianDao(username, money);
	}
	public int  yue(String username) {
		AtmDao ad=new AtmDao();
		return ad.chaxunyue(username);
	}
	public void queryAll(int start,int end){
		AtmDao ad=new AtmDao();
		for(Object arr: ad.queryAllDao(start, end)){
			System.out.println((Atm)arr);
		}
	}
}

6.测试

package test;
import Service.AtmService;
public class Test {
	public static void main(String[] args) {
		AtmService as=new AtmService();
		System.out.println("马云的存款前余额为:"+as.yue("马云"));
		as.cunkuan("马云", 10000);
		System.out.println("马云的存款后余额为:"+as.yue("马云"));
		System.out.println("小王收款前余额为:"+as.yue("小王"));
		as.zhuanzhang("马云", 2000, "小王");
		System.out.println("马云的转账后余额为:"+as.yue("马云"));
		System.out.println("小王收款后余额为:"+as.yue("小王"));
		as.qukuan("小王", 1000);
		System.out.println("小王取款后余额为:"+as.yue("小王"));
		as.queryAll(1, 2);
	}
}

 

你可能感兴趣的:(oracle数据库,java,jdbc)