数据库的连接与简单的ATM增删查改操作

来源:互联网 发布:室内装修模拟软件 编辑:程序博客网 时间:2024/06/07 01:05

连接数据库有两种方式,直连和桥连,这里作者只讲一下普遍的直连,桥连读者想知道请自行百度

首先要有驱动软件,笔者的工具很旧

在你的java项目里面创建一个文件夹lib,将jar文件复制到里面,右键点击jar文件→Build Path→Configure Build Path→

添加找到lib目录下的jar文件 确定 则会出现奶瓶,这样驱动就弄好的,下面开始连接







请注意里面所有的包都是java.sql下的 ,其中root是你的mysql账号,610321520是你的mysql的密码,3306是mysql的端口号,localhost是回文地址也可写为127.0.0.1


//加载驱动
Class.forName("com.mysql.jdbc.Driver");

//注册驱动
Connection coo=DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","610321520");

 //产生语句处理对象

                Statement sta=coo.createStatement();;连接数据库的重要组成部分


下面就是我做的一个简单的ATM连接数据库

首先在mysql里面创建书库

CREATE DATABASE bank
USE bank
CREATE TABLE users(
u_id INT,
u_name VARCHAR(50),
u_password INT,
u_money DOUBLE)
INSERT INTO users VALUES(1,'小王',111,5000)
INSERT INTO users VALUES(2,'小花',222,5000)
INSERT INTO users VALUES(3,'小明',333,5000)

下面是java代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;


public class ATM {
public static void main(String[] args) {
Connection con = null;
Statement sta = null;
ResultSet rs = null;
int yh;
int mm;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


String url = "jdbc:mysql://localhost:3306/bank";
try {
System.out.println("系统正常运行");
con = DriverManager.getConnection(url, "root", "610321520");
// System.out.println(con);
sta = con.createStatement();
System.out.println("欢迎来到中国银行");
int count = 0;
while(true) {
System.out.println("请输入账号");
Scanner in = new Scanner(System.in);
yh = in.nextInt();
System.out.println("请输入密码");
Scanner in1 = new Scanner(System.in);
mm = in.nextInt();
String sql = "select * from users where u_id = '"+yh+"' and u_password = '"+mm+"'";
rs = sta.executeQuery(sql);
if(rs.next()) {
break;
}else {
count ++ ;
if(count == 3) {
System.out.println("您输入的次数已超过安全次数,请携带有效证件去柜台取卡");
System.exit(0);
}else {
System.out.println("用户名或密码不正确!");
}
}
}

while(true) {
String s = "select u_name from users where u_id = '"+yh+"'";
rs = sta.executeQuery(s);
   String name = null ;
if(rs.next()) {
name = rs.getString("u_name");
System.out.println(name+"先生您好!请选择操作按钮:1.存款 2.取款 3.查询余额 4.修改密码 5.转账  6.退卡");
}
Scanner in = new Scanner(System.in);
int flag = in.nextInt();
if(flag == 1) {
System.out.println("请输入您的存款金额");
Scanner in1 = new Scanner(System.in);
int c_money = in1.nextInt();
String sql = "update users set u_money=u_money + '"+c_money+"' where u_id='"+yh+"'";
sta.executeUpdate(sql);
System.out.println("存款成功");
}else if(flag == 2) {
System.out.println("请输入你的取款金额");
Scanner in1 = new Scanner(System.in);
int q_money =in1.nextInt();
String  sql1 = "select u_money from users where u_id='"+yh+"'";
rs = sta.executeQuery(sql1);
int money = 0 ;
while(rs.next()) {
money = rs.getInt(1);
}
if(money >= q_money) {
String sql = "update users set u_money=u_money - '"+q_money+"' where u_id='"+yh+"'";
sta.executeUpdate(sql);
System.out.println("取款成功");

}else {
System.out.println("您的余额不足,请查询余额");
}

}else if(flag == 3) {
String  sql1 = "select u_money from users where u_id='"+yh+"'";
rs = sta.executeQuery(sql1);
int money = 0 ;
while(rs.next()) {
money = rs.getInt(1);
}
System.out.println("您的余额为:" + money + "元");

}else if(flag == 4) {
System.out.println("请输入旧密码");
Scanner in1 = new Scanner(System.in);
int jmm = in1.nextInt();
if(jmm == mm) {
System.out.println("请输入新密码");
Scanner in2 = new Scanner(System.in);
int xmm = in1.nextInt();
String sql = "update users set u_password='"+xmm+"' where u_id='"+yh+"'";
sta.executeUpdate(sql);
System.out.println("修改密码成功");
}else {
System.out.println("您输入的密码有误,请重新输入");
}
}else if(flag == 5) {
System.out.println("请输入您要转帐的用户");
Scanner in1 = new Scanner(System.in);
String zh1 = in1.next();
System.out.println("请再次输入您要转帐的用户");
Scanner in2 = new Scanner(System.in);
String zh2 = in1.next();
if(zh1.equals(zh2)) {
String sql = "select u_name from users where u_id='"+yh+"'";
rs = sta.executeQuery(sql);
if(rs.next()) {
System.out.println("请输入您要转账的金额");
Scanner in3 = new Scanner(System.in);
int z_money = in3.nextInt();
String  sql1 = "select u_money from users where u_id='"+yh+"'";
rs = sta.executeQuery(sql1);
int money = 0 ;
while(rs.next()) {
money = rs.getInt(1);
}
if(money >= z_money) {
String sql2 = "update users set u_money = u_money-'"+z_money+"' where u_id='"+yh+"'";
sta.executeUpdate(sql2);
String sql3 = "update users set u_money = u_money+'"+z_money+"' where u_id='"+zh1+"'";
sta.executeUpdate(sql3);
System.out.println("转账成功");
}else {
System.out.println("您的余额不足");
}
}else {
System.out.println("您输入的账户不存在");
}
}else {
System.out.println("您输入的账户不一致,请重新输入");
}
}else if(flag == 6) {
System.out.println("谢谢使用!");
System.exit(0);
}else {
System.out.println("暂未开通此服务,敬请期待!");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs != null) {
rs.close();
}
if(sta != null) {
sta.close();
}
if(con != null) {
con.close();
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}

1 0
原创粉丝点击