Java基于JDBC操作MYSQL进行增删改查登陆注册完整代码

java基于jdbc对mysql数据库进行增删改查登陆注册的完整示例代码。新手入门代码,不足之处请评论指出。
用的是Scanner在控制面板输入参数和返回参数。



1、创建Java Project项目
Java基于JDBC操作MYSQL进行增删改查登陆注册完整代码_第1张图片
2、导入Jar包

mysql-connector-java-5.0.8-bin.jar


3、创建数据库

//根据实体类创建数据库字段和类型


4、创建实体类(BankModel.java)

package com.model;

public class BankModel {

	    private int id;

	    private String users;

	    private String money;

	    private String telephone;

	    private int cardno;

	    private String address;

	    private String remark;

	    private int isok;

	    public int getId() {
	      return id;
	    }

	    public void setId(int  id) {
	      this.id = id;
	    }

	    public String getUsers() {
	      return users;
	    }

	    public void setUsers(String  users) {
	      this.users = users;
	    }

	    public String getMoney() {
	      return money;
	    }

	    public void setMoney(String  money) {
	      this.money = money;
	    }

	    public String getTelephone() {
	      return telephone;
	    }

	    public void setTelephone(String  telephone) {
	      this.telephone = telephone;
	    }

	    public int getCardno() {
	      return cardno;
	    }

	    public void setCardno(int  cardno) {
	      this.cardno = cardno;
	    }

	    public String getAddress() {
	      return address;
	    }

	    public void setAddress(String  address) {
	      this.address = address;
	    }

	    public String getRemark() {
	      return remark;
	    }

	    public void setRemark(String  remark) {
	      this.remark = remark;
	    }

	    public int getIsok() {
	      return isok;
	    }

	    public void setIsok(int  isok) {
	      this.isok = isok;
	    }
}


5、创建数据库驱动类(DBUtil.java)

package com.dbutil;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {

	public static Connection getConnection() {
		Connection conn=null;		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/heizhu?useSSL=false","root","1234");						
		} catch (Exception e) {
			e.printStackTrace();
		}				
		return conn;		
	}
	
}


6、创建数据库操作类(BankDao.java)

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.dbutil.DBUtil;
import com.model.BankModel;

public class BankDao {

	// 查询所有
	public ArrayList getList() {
		// 存储从数据库中取出来的数据
		ArrayList ar = new ArrayList();
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select * from bank";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();// 执行数据库查询的方法,放到rs中
			while (rs.next()) {
				BankModel bankm = new BankModel();
				bankm.setId(rs.getInt("id"));
				bankm.setUsers(rs.getString("users"));
				bankm.setMoney(rs.getString("money"));
				bankm.setTelephone(rs.getString("telephone"));
				bankm.setCardno(rs.getInt("cardno"));
				bankm.setAddress(rs.getString("address"));
				bankm.setRemark(rs.getString("remark"));
				bankm.setIsok(rs.getInt("isok"));
				ar.add(bankm);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 当数据库使用后必须关闭,如果没有关闭数据库的接口有限,下次就不能连接
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ar;

	}

	// 查询单个
	public BankModel select(int id) {		
		BankModel bankm=null;
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;	
		ResultSet rs = null;
		String sql = "SELECT * FROM bank  WHERE id=?";				
		try {
			ps = conn.prepareStatement(sql);			
			ps.setInt(1, id);
			rs = ps.executeQuery(); //执行数据库查询的方法,放到rs中
			while (rs.next()) {
				bankm = new BankModel();
				bankm.setId(rs.getInt("id"));
				bankm.setUsers(rs.getString("users"));
				bankm.setMoney(rs.getString("money"));
				bankm.setTelephone(rs.getString("telephone"));
				bankm.setCardno(rs.getInt("cardno"));
				//bankm.setAddress(rs.getString("address"));
				//bankm.setRemark(rs.getString("remark"));
				bankm.setIsok(rs.getInt("isok"));
			}			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return bankm;
	}

	// 添加用户
	public void inset(BankModel Bankm) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into bank(users,money,telephone,cardno,address,remark,isok)values(?,?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			// 这个方法用于改变数据库数据,a代表改变数据库的条数
			ps.setString(1, Bankm.getUsers());
			ps.setString(2, Bankm.getMoney());
			ps.setString(3, Bankm.getTelephone());
			ps.setInt(4, Bankm.getCardno());
			ps.setString(5, Bankm.getAddress());
			ps.setString(6, Bankm.getRemark());
			ps.setInt(7, Bankm.getIsok());
			int a = ps.executeUpdate();
			if (a > 0) {
				System.out.println("添加成功");
			} else {
				System.out.println("添加失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			// 关闭
			if (ps != null) {
				ps.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}

	// 删除 把状态改成0
	public void delete(int id) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update bank set isok=0 where id=?"; //DELETE FROM bank WHERE id=?;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			int a = ps.executeUpdate();
			if (a > 0) {
				System.out.println("注销成功");
			} else {
				System.out.println("注销失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	// 修改 jia
	public void updatejia(BankModel Bankm) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update bank set money=money+? where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, Bankm.getMoney());
			ps.setInt(2, Bankm.getId());
			int a = ps.executeUpdate();
			if (a > 0) {
				System.out.println("存入成功");
			} else {
				System.out.println("存入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	// 修改 jian
	public void updatejian(BankModel Bankm) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update bank set money=money-? where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, Bankm.getMoney());
			ps.setInt(2, Bankm.getId());
			int a = ps.executeUpdate();
			if (a > 0) {
				System.out.println("取钱成功");
			} else {
				System.out.println("取钱失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	
	//用户登陆
	public void login(BankModel Bankm) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet  rs=null;
		String sql = "select * from bank where users=? and telephone=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, Bankm.getUsers());
			ps.setString(2, Bankm.getTelephone());
			rs = ps.executeQuery();
			boolean ok=rs.next();			
			if (ok) {
				System.out.println("登陆成功");
			} else {
				System.out.println("用户名或密码错误");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	
	
}


7、前端页面(Test.java)

package com.test;

import java.util.ArrayList;
import java.util.Scanner;

import com.dao.BankDao;
import com.model.BankModel;

public class Test {

	public static void main(String[] args) {
	
		@SuppressWarnings("resource")
		Scanner sc=new Scanner(System.in);
		BankDao bankd=new BankDao();
		BankModel bankm =new BankModel();   		
		while(true) {
			System.out.println("*****************中国人民银行*******************");			
			System.out.print("        1、所有信息");
        	System.out.println("                                  2、现金存入");
        	System.out.print("        3、现金取出");
        	System.out.println("                                  4、用户注册");
        	System.out.print("        5、注销用户");
        	System.out.println("                                  6、单个信息");
        	System.out.print("        7、用户登陆");
        	System.out.println("                                  8、退出程序");       	
        	System.out.println("*****************中国人民银行*******************");
        	
        	System.out.println("请输入功能号:");
        	int a=sc.nextInt();
        	if(a==1) {//查询
        		ArrayList ar=new BankDao().getList();
        		for(BankModel ne:ar) { 	      			
        			System.out.println("----编号:"+ne.getId()+"----用户名:"+ne.getUsers()+"----电话:"+ne.getTelephone()+"----卡号:"+ne.getCardno()+"----余额:"+ne.getMoney()+"----状态"+ne.getIsok()+"----\r");
        	}
        		
        	}else if(a==2) {//存款-修改 	 
        		System.out.println("请输入存款编号");        		
        		bankm.setId(sc.nextInt());       		
        		System.out.println("请输入存入金额");           	  		
        		bankm.setMoney(sc.next());
        		bankd.updatejia(bankm);
        		
        	}else if(a==3) { //取款-修改
        		System.out.println("请输入取款编号");        		
        		bankm.setId(sc.nextInt());       		
        		System.out.println("请输入取出金额");           	  		
        		bankm.setMoney(sc.next());		
        		bankd.updatejian(bankm);
        		
        	}else if(a==4) { //添加用户 --插入        		
        		System.out.println("请输入用户名");
        		bankm.setUsers(sc.next());
        		System.out.println("请输入预存款");
        		bankm.setMoney(sc.next());
        		System.out.println("请输入电话号码");
        		bankm.setTelephone(sc.next());
        		System.out.println("请输入银行卡号");
        		bankm.setCardno(sc.nextInt());
        		System.out.println("请输入家庭地址");
        		bankm.setAddress(sc.next());
        		System.out.println("请输入开户行");
        		bankm.setRemark(sc.next());
        		System.out.println("是否立即启用 1是 0否");
        		bankm.setIsok(sc.nextInt());    
        		bankd.inset(bankm);   
        		
        	}else if(a==5) { //删除用户        		
        		System.out.println("请输入需要删除的用户编号");
        		int id=sc.nextInt();
        		bankd.delete(id);     
        		
        	}else if(a==6) { //查询单个用户	
        		System.out.println("请输入需要查询的用户编号");
        		int id=sc.nextInt();
        		BankModel bankm1= bankd.select(id);   		
        		System.out.println("----编号:"+bankm1.getId()+"----用户名:"+bankm1.getUsers()+"----电话"+bankm1.getTelephone()+"----卡号:"+bankm1.getCardno()+"----余额:"+bankm1.getMoney()+"----状态"+bankm1.getIsok()+"----\r");
        		
        	}else if(a==7) { //用户登陆
        		System.out.println("请输入用户名");
        		bankm.setUsers(sc.next());
        		System.out.println("请输入电话号码");
        		bankm.setTelephone(sc.next());
        		bankd.login(bankm);		
        	}else if(a==8) {
        		System.out.println("退出程序成功");
        		break;
        	}else {
        		System.out.println("功能开发中。。");
        	}
		}
		
	}

}

 

你可能感兴趣的:(Java)