2、JDBC面向对象方式实现数据库CRUD操作

已知

:建立表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分。

StudentDao:

import java.sql.*;
import java.text.SimpleDateFormat;

public class StudentDao {
	
	Connection con;
	PreparedStatement pst;
	ResultSet rs;
	
	//连接数据库
	public Connection getConnection() throws SQLException, ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		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();
		}
	}
	
	//1、向表中增加记录并显示所有记录(数据自己指定)
	public void addStudent(String name, java.sql.Date date, float score) throws ClassNotFoundException, SQLException {
		con = getConnection();
		
		String sqladd = "INSERT INTO student VALUES(NULL,?,?,?)";
		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 ? "数据插入成功":"数据插入失败");
		
		closeAll();
	}
	
	//2、从表中删除id=1的记录并显示所有记录
	public void del_By_id(int id) throws ClassNotFoundException, SQLException {
		con = getConnection();
		
		String sql = "DELETE FROM student WHERE id = ?";
		pst = con.prepareStatement(sql);
		pst.setInt(1, id);
		int result = pst.executeUpdate();
		System.out.println(result > 0 ? "数据删除成功":"数据不存在");
		
		closeAll();
	}
	
	//3、修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
	public void updatebyid(int id, String name) throws ClassNotFoundException, SQLException {
		con = getConnection();
		
		String update = "update student set name=? WHERE id=?";
		pst = con.prepareStatement(update);
		pst.setString(1, name);
		pst.setInt(2, id);
		int result = pst.executeUpdate();
		System.out.println(result > 0 ? "信息修改成功" : "信息修改失败");
		
		closeAll();
	}
	//显示全部信息
	public void selectAll() throws SQLException, ClassNotFoundException {
		con = getConnection();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		String sql = "select * from student";
		pst = con.prepareStatement(sql);
		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));
		}
		
		closeAll();
	}
	
	//4、查询表中id=3的记录并显示
	public void selectByid(int id) throws ClassNotFoundException, SQLException {
		con = getConnection();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		String sqlselect = "SELECT * FROM student WHERE id = ?";
		pst = con.prepareStatement(sqlselect);
		pst.setInt(1, id);
		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("该学生不存在");
		
		closeAll();
	}
}

Test:

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Test {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		Scanner input = new Scanner(System.in);
		StudentDao sd = new StudentDao();
		int id;
		float score;
		String name;
		Date schoolin;
		java.sql.Date date;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			//1、向表中增加记录并显示所有记录(数据自己指定)
			System.out.println("请输入学生姓名、入学时间、成绩");
			name = input.next();
			schoolin = sdf.parse(input.next());
			date = new java.sql.Date(schoolin.getTime());
			score = input.nextFloat();
			sd.addStudent(name,date,score);
			System.out.println();

			//2、从表中删除id=1的记录并显示所有记录
			System.out.println("请输入需要删掉的 id ");
			id = input.nextInt();
			sd.del_By_id(id);
			System.out.println();
			
			//3、修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
			System.out.println("请输入需要修改学生的id");
			id = input.nextInt();
			System.out.println("请输入修改后的姓名");
			name = input.next();
			sd.updatebyid(id, name);
			System.out.println();
			System.out.println("显示全部信息");
			sd.selectAll();
			System.out.println();
			
			//4、查询表中id=3的记录并显示
			System.out.println("请输入需要查询的id");
			id = input.nextInt();
			sd.selectByid(id);
			System.out.println();
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		input.close();	
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值