1、JDBC单表记录的增删改查
(20分)
已知:建立表student,并向表里插入几条记录,
create table student(
id int PRIMARY KEY auto_increment,
name varchar(20) not null,
schoolin date not null,
score float not null);
insert into student values(null,’李丽’,’2015-09-01’,86);
insert into student values(null,’王五’,’2016-09-01’,99);
insert into student values(null,’张三’,’2014-09-01’,88);
要求:用JAVA程序实现如下功能:
1、向表中增加记录并显示所有记录(数据自己指定);
2、从表中删除id=1的记录并显示所有记录;
3、修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
4、查询表中id=3的记录并显示;
提交说明:粘贴JAVA程序代码;
难度:20
评价标准:
(1)程序的基本结构明确,4分;
(2)增加功能实现,4分;
(3)删除功能实现,4分;
(4)修改功能实现,4分;
(5)查询单条记录实现,4分。
共计20分。
代码:
import java.sql.*;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException, ParseException {
Scanner input = new Scanner(System.in);
int id;
float score;
String name;
Date schoolin;
java.sql.Date date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//1、向表中增加记录并显示所有记录(数据自己指定)
System.out.println("请输入学生姓名、入学时间、成绩");
name = input.next();
schoolin = sdf.parse(input.next());
date = new java.sql.Date(schoolin.getTime());
//可将java.util.Date 转化为 java.sql.Date
score = input.nextFloat();
addStudent(name,date,score);
System.out.println();
//2、从表中删除id=1的记录并显示所有记录
System.out.println("请输入需要删掉的 id ");
id = input.nextInt();
del_By_id(id);
System.out.println();
//3、修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
System.out.println("请输入需要修改学生的id");
id = input.nextInt();
System.out.println("请输入修改后的姓名");
name = input.next();
updatebyid(id, name);
System.out.println();
System.out.println("显示全部信息");
selectAll();
//4、查询表中id=3的记录并显示
System.out.println("请输入需要查询的id");
id = input.nextInt();
selectByid(id);
System.out.println();
input.close();
}
2、JDBC面向对象方式实现数据库CRUD操作(30分)
已知:建立表student,并向表里插入几条记录。
create table student(
id int PRIMARY KEY auto_increment,
name varchar(20) not null,
schoolin date not null,
score float not null);
insert into student values(null,’李丽’,’2015-09-01’,86);
insert into student values(null,’王五’,’2016-09-01’,99);
insert into student values(null,’张三’,’2014-09-01’,88);
基本要求:将表操作封装成类,将功能封装成类的方法。
功能要求:
(1)向表中增加记录并显示所有记录(数据自己指定);
(2)从表中删除id=1的记录并显示所有记录;
(3)修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
(4)查询表中id=3的记录并显示。
提交说明:粘贴JAVA程序代码。
难度:30
评价标准:
(1)类的层次设计合理,5分;
(2)增加功能实现,5分;
(3)删除功能实现,5分;
(4)修改功能实现,5分;
(5)查询单条记录实现,5分。
(6)查询所有记录实现,5分。
共计30分。
//1、向表中增加记录并显示所有记录(数据自己指定)
private static void addStudent(String name, java.sql.Date date, float score) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8","root","usbw");
// ?useUnicode=true&characterEncoding=utf-8 输入会乱码
String sqladd = "INSERT INTO student VALUES(NULL,?,?,?)";
PreparedStatement pst = con.prepareStatement(sqladd);
pst.setString(1, name);
pst.setDate(2, date);
pst.setFloat(3, score);
//数字代表第几个'?'
int result = pst.executeUpdate();
System.out.println(result > 0 ? "数据插入成功":"数据插入失败");
pst.close();
con.close();
}
//2、从表中删除id=1的记录并显示所有记录
private static void del_By_id(int id) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8","root","usbw");
String sql = "DELETE FROM student WHERE id = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(1, id);
int result = pst.executeUpdate();
System.out.println(result > 0 ? "数据删除成功":"数据不存在");
pst.close();
con.close();
}
//3、修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
private static void updatebyid(int id, String name) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8","root","usbw");
String update = "update student set name=? WHERE id=?";
PreparedStatement pst = con.prepareStatement(update);
pst.setString(1, name);
pst.setInt(2, id);
int result = pst.executeUpdate();
System.out.println(result > 0 ? "信息修改成功" : "信息修改失败");
pst.close();
con.close();
}
显示所有记录
private static void selectAll() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8","root","usbw");
String sql = "select * from student";
PreparedStatement pst = con.prepareStatement(sql);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//记录指针,初始化在第一条数据之前
ResultSet rs = pst.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String schoolin = sdf.format(rs.getDate("schoolin"));
float score = rs.getFloat("score");
System.out.println(id+" "+name+" "+schoolin+" "+String.format("%.2f", score));
}
System.out.println();
rs.close();
pst.close();
con.close();
}
//4、查询表中id=3的记录并显示
private static void selectByid(int id) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8","root","usbw");
String sqlselect = "SELECT * FROM student WHERE id = ?";
PreparedStatement pst = con.prepareStatement(sqlselect);
pst.setInt(1, id);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ResultSet rs = pst.executeQuery();
if(rs.next()) {
String name = rs.getString("name");
String schoolin = sdf.format(rs.getDate("schoolin"));
float score = rs.getFloat("score");
System.out.println(id+" "+name+" "+schoolin+" "+String.format("%.2f", score));
}
else
System.out.println("该学生不存在");
rs.close();
pst.close();
con.close();
}
}
3、简易版银行管理管理(50分)
已知:现有账户表account,创建的SQL语句如下。
create table account(
id int PRIMARY KEY auto_increment,
name varchar(20) not null,
money double not null);
基本要求:
(1)利用面向对象方式实现功能;
(2)利用三层架构实现;
(3)应用事务处理。
功能要求:
(1)开户——增加记录
增加3个账户,姓名分别为:张三、李四、王五,账户初始金额:0元;
开户成功,显示所有账户信息。
(2)销户——删除记录
对姓名为"王五"的账户给予销户。成功操作后,显示所有账户信息。
(3)存钱——修改记录
张三、李四账户分别存入2000元。成功操作后,显示所有账户信息。
(4)取钱——修改记录
张三账户取出1000元,显示张三账户信息。
(5)转账——修改记录
李四给张三转账500元,显示张三和李四账户信息。
(6)查询所有账户信息。
提交说明:粘贴JAVA程序代码。
评价标准:
(1)类的层次设计合理,程序三层架构搭建合理,10分;
(2)开户与销户功能实现,10分;
(3)存钱与取钱功能实现,10分。
(4)应用事务实现转账功能实现,20分。
用户交互层
package cn.sdut.amain;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import cn.sdut.biz.BankBiz;
import cn.sdut.entity.Account;
//用户交互层
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BankBiz biz=new BankBiz();
double money;
String name;
int id,choose;
boolean exit = false;
System.out.println("请选择需要的选项\n(请输入相应数字)");
System.out.println("\n\t*********************************");
System.out.println("\t*\t\t\t\t*");
System.out.println("\t*\t 1 - 开户\t\t*");
System.out.println("\t*\t 2 - 销户\t\t*");
System.out.println("\t*\t 3 - 存钱\t\t*");
System.out.println("\t*\t 4 - 取钱\t\t*");
System.out.println("\t*\t 5 - 转账\t\t*");
System.out.println("\t*\t 6 - 显示所有信息\t*");
System.out.println("\t*\t 7 - 退出\t\t*");
System.out.println("\t*\t\t\t\t*");
System.out.println("\t*********************************\n\n");
while(input.hasNext())
{
choose = input.nextInt();
try {
switch(choose) {
case 1:{
System.out.println("请输入户主姓名和初始金额");
name = input.next();
money = input.nextDouble();
boolean flag = biz.openAccount(name, money);
if(flag)
System.out.println("开户成功");
List<Account> selectAll = biz.selectAll();
for(Account acc:selectAll) {
System.out.println(acc);
}
break;
}
case 2:{
System.out.println("请输入需要销户的账号");
id = input.nextInt();
boolean flag = biz.distoryAccount(id);
if(flag)
System.out.println("销户成功");
List<Account> selectAll = biz.selectAll();
for(Account acc:selectAll) {
System.out.println(acc);
}
break;
}
case 3:{
System.out.println("请输入需要存钱的账号");
id = input.nextInt();
money = input.nextDouble();
boolean flag = biz.saveMoney(id, money);
if(flag)
System.out.println("存入成功");
List<Account> selectAll = biz.selectAll();
for(Account acc:selectAll) {
System.out.println(acc);
}
break;
}
case 4:{
System.out.println("请输入需要取钱的账号");
id = input.nextInt();
money = input.nextDouble();
boolean flag = biz.takeMoney(id, money);
if(flag)
System.out.println("取出成功");
System.out.println(biz.search_id(id));
break;
}
case 5:{
System.out.println("请输入转账账号及金额");
int idOut = input.nextInt();
int idIn = input.nextInt();
money = input.nextDouble();
boolean flag=biz.trunMoney(idOut,idIn ,money);
if(flag)
System.out.println("转账成功");
System.out.println(biz.search_id(idOut));
System.out.println(biz.search_id(idIn));
break;
}
case 6:{
System.out.println("显示全部信息");
List<Account> selectAll = biz.selectAll();
for(Account acc:selectAll) {
System.out.println(acc);
}
break;
}
case 7:{
System.out.println("是否退出 Yes/No");
if(input.next().equals("Yes"))
exit = true;
break;
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(exit) {
System.out.println("程序已退出");
break;
}
}
input.close();
}
}
业务逻辑层
package cn.sdut.biz;
import java.sql.SQLException;
import java.util.List;
import cn.sdut.dao.AccountDao;
import cn.sdut.entity.Account;
//业务逻辑层
public class BankBiz {
AccountDao accDao=new AccountDao();
//开户
public boolean openAccount(String name,double Money) throws ClassNotFoundException, SQLException
{
int result = accDao.addAccount(name, Money);
return result>0?true:false;
}
//销户
public boolean distoryAccount(int id) throws ClassNotFoundException, SQLException {
int result = accDao.delAccount(id);
return result>0?true:false;
}
//存钱
public boolean saveMoney(int id,double money) throws ClassNotFoundException, SQLException {
return accDao.updateAccount(id, money) > 0 ? true:false;
}
//取钱
public boolean takeMoney(int id,double money) throws ClassNotFoundException, SQLException {
return accDao.updateAccount(id, -1*money) > 0 ? true:false;
}
//转账
public boolean trunMoney(int accidIn,int accidOut,double change) throws ClassNotFoundException, SQLException {
return accDao.TrunMoney(accidIn, accidOut, change) > 0 ? true:false;
}
//查询所有
public List<Account> selectAll() throws ClassNotFoundException, SQLException {
return accDao.selectAll();
}
//根据id查询
public Account search_id(int id) throws ClassNotFoundException, SQLException {
return accDao.selectById(id);
}
}
数据库访问层AccountDao
package cn.sdut.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import cn.sdut.entity.Account;
//数据库访问层
public class AccountDao extends BaseDao {
//开户
public int addAccount(String name, double money) throws ClassNotFoundException, SQLException {
//创建数据库链接
con=getConnection();
//准备SQL语句
String sql = "insert into account values(null,?,?)";
//执行
pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setDouble(2, money);
int result = pst.executeUpdate();
//关闭连接
closeAll();
return result;
}
//销户
public int delAccount(int id) throws ClassNotFoundException, SQLException {
//创建连接
con=getConnection();
//准备SQL语句
String sql = "delete from account where id=?";
//执行
pst = con.prepareStatement(sql);
pst.setInt(1,id);
int result = pst.executeUpdate();
//关闭连接
closeAll();
return result;
}
//根据id修改信息
public int updateAccount(int id,double change) throws ClassNotFoundException, SQLException {
//创建连接
con=getConnection();
//准备SQL语句
String sql = "update account set money=money+? where id=?";
//执行
pst = con.prepareStatement(sql);
pst.setDouble(1,change);
pst.setInt(2,id);
int result = pst.executeUpdate();
//关闭连接
closeAll();
return result;
}
//显示全部信息
public List<Account> selectAll() throws ClassNotFoundException, SQLException {
//创建list储存信息
List<Account> accountList=new ArrayList<Account>();
//创建连接
con=getConnection();
//准备SQL语句
String sql = "select * from account";
//执行
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next())
{
Account account=new Account();
account.setId(rs.getInt("ID"));
account.setName(rs.getString(2));
account.setMoney(rs.getDouble("Money"));
accountList.add(account);
}
//关闭
closeAll();
return accountList;
}
//根据id查询信息
public Account selectById(int id) throws ClassNotFoundException, SQLException
{
//查询对象
Account account=null;
//连接
con=getConnection();
//SQL语句
String sql = "select * from account where id=?";
//执行
pst = con.prepareStatement(sql);
pst.setInt(1, id);
rs = pst.executeQuery();
if(rs.next())
{
account=new Account();
account.setId(rs.getInt("ID"));
account.setName(rs.getString(2));
account.setMoney(rs.getDouble("money"));
}
//关闭
closeAll();
return account;
}
//根据id转账
public int TrunMoney(int idOut,int idIn,double change) throws ClassNotFoundException, SQLException {
con = getConnection();
//开启事务管理,禁止自动提交(默认自动提交)
con.setAutoCommit(false);
//SQL语句
String sql = "update account set money=money+? where id=?";
//捆绑转入转出操作
//放置缓冲区,确定成功再修改数据库
//转入操作
pst = con.prepareStatement(sql);
pst.setDouble(1,change);
pst.setInt(2,idIn);
int result1 = pst.executeUpdate();
//转出操作
pst = con.prepareStatement(sql);
pst.setDouble(1,-1*change);
pst.setInt(2,idOut);
int result2 = pst.executeUpdate();
//判断
if(result1*result2 > 0)
con.commit();
//传入数据,修改数据库
else
con.rollback();
//数据回滚,不提交
closeAll();
return result1*result2;
}
}
BaseDao
package cn.sdut.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
//定义3个属性,在数据库操作中常用
Connection con;
PreparedStatement pst;
ResultSet rs;
//获得数据库的连接
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8", "root", "usbw");
return con;
}
//关闭公共对象
public void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Account 类
package cn.sdut.entity;
public class Account {
private int id;
private String name;
private double money;
public Account() {
super();
}
public Account(int id, String name, double money) {
super();
this.id = id;
this.name = name;
this.money = money;
}
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 double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "\t账号:" + id + "\t户主姓名:" + name + "\t账户余额:" + money;
}
}