JDBC案例
一、案例需求
创建数据库,新建学生信息表,至少5个字段,完成下列的功能:
1、创建主键,自增长。
2、用JDBC实现添加学生的功能,要求,学生除学号之外的信息全部从控制台接收用户输入,并提示添加成功还是失败!
3、用JDBC实现删除学生的功能,要求用户输入要删除的学生的学号,并提示删除成功还是失败!
4、用JDBC实现实现根据学生的学号修改学生信息的功能,要修改的学生的学号和学生的新信息都从控制台接收用户输入
5、实现查看学生详情的功能,控制台接受用户查看学生的ID,根据ID显示这个学生所有的详细信息。
6、完善添加功能,对用户输入的学生信息进行完整性验证,用户输入的数据不符合数据库建表的约束时提示错误信息
备注:数据库的连接信息通过properties访问,properties通过PropertiesUtil操作。
二、实现
2.1 properties文件
file.properties
DRIVER = com.mysql.jdbc.Driver
URL = jdbc:mysql://127.0.0.1:3306/abc?useUnicode=true&characterEncoding=UTF-8
USER = root
PWD = root
2.2数据库创建student表
CREATE TABLE student(
stu_id int PRIMARY key auto_increment,
stu_name VARCHAR(20),
stu_sex VARCHAR(20)
);
2.3PropertiesUtil工具类
import java.io.File;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
public class PropertiesUtil {
private static String DRIVER = null;
private static String URL = null;
private static String USER = null;
private static String PWD = null;
//1.建立连接器
public static void getConnection(){
File f = new File("day05/src/homework/file.properties");
Properties p = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
p.load(fis);
DRIVER = p.getProperty("DRIVER");
URL = p.getProperty("URL");
USER = p.getProperty("USER");
PWD = p.getProperty("PWD");
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//2.增加功能
public static void insertdata(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入您要添加的学生姓名:");
String name = sc.nextLine();
if (name.length()>20){
System.out.println("输入错误!");
return;
}
System.out.print("请输入您要添加的学生性别:");
String sex = sc.nextLine();
if (sex.length()>20){
System.out.println("输入错误!");
return;
}
Connection con =null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PWD);
String sql = "insert into student VALUES (null,?,? )";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,sex);
int i = ps.executeUpdate();//受影响的行
if (i>0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (con!=null){
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//3.删除功能
public static void deletedata(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入您要删除的学生ID:");
int id = sc.nextInt();
Connection con =null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PWD);
String sql = "DELETE FROM student WHERE stu_id=?;";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,id);
int i = ps.executeUpdate();//受影响的行
if (i>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (con!=null){
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//4.更新功能
public static void update(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入您要修改的学生ID:");
int id = sc.nextInt();
System.out.print("请输入您要修改的学生姓名:");
String stu_name = sc.next();
System.out.print("请输入您要修改的学生性别:");
String stu_sex = sc.next();
Connection con =null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PWD);
String sql = "UPDATE student set stu_name = ?,stu_sex= ? WHERE stu_id =?;";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,stu_name);
ps.setString(2,stu_sex);
ps.setInt(3,id);
int i = ps.executeUpdate();//受影响的行
if (i>0){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (con!=null){
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//5.查找功能
public static void selectdata(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入您要查看的学生ID:");
int id = sc.nextInt();
Connection con= null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PWD);
String sql ="SELECT * from student WHERE stu_id =? " ;
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();//查找的行,Query查询
if (rs.next()){
System.out.println("查找的学生信息如下:");
System.out.println("学生ID"+"\t"+"姓名"+"\t"+"性别");
System.out.print(rs.getString("stu_id")+"\t");
System.out.print(rs.getString("stu_name")+"\t");
System.out.print(rs.getString("stu_sex")+"\t");
System.out.println();
}else{
System.out.println("抱歉!数据库内没有此学生的信息。");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (con !=null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.4Student测试类
import java.util.Scanner;
public class Student {
public static void main(String[] args) {
PropertiesUtil.getConnection();
boolean isWork=true;
Scanner sc = new Scanner(System.in);
System.out.println("*************************************");
System.out.println("********欢迎使用学生信息管理系统********");
System.out.println("********1.添加学生信息*****************");
System.out.println("********2.删除学生信息*****************");
System.out.println("********3.修改学生信息*****************");
System.out.println("********4.查找学生信息*****************");
System.out.println("********5.退出学生系统*****************");
System.out.println("**************************************");
System.out.println();
while (isWork){
System.out.print("请选择您要使用的功能:");
int choseNum = sc.nextInt();
if (choseNum==1){
System.out.println("您选择的是添加学生信息功能");
PropertiesUtil.insertdata();
}else if (choseNum==2){
System.out.println("您选择的是删除学生信息功能");
PropertiesUtil.deletedata();
}else if (choseNum==3){
System.out.println("您选择的是修改学生信息功能");
PropertiesUtil.update();
}else if (choseNum==4){
System.out.println("您选择的是查找学生信息功能");
PropertiesUtil.selectdata();
}else if (choseNum==5){
System.out.println("您选择的是退出学生系统");
System.out.println("即将退出.......");
isWork = false;
}
}
}
}