大数据之路week05--day01(JDBC 初识之实现一个系统 实现用户选择增删改查 未优化版本)

要求,实现用户选择增删改查。

给出mysql文件,朋友们可以自己运行导入到自己的数据库中:

/*
Navicat MySQL Data Transfer Source Server : mysql
Source Server Version : 50557
Source Host : 127.0.0.1:3306
Source Database : school Target Server Type : MYSQL
Target Server Version : 50557
File Encoding : 65001 Date: 2019-12-02 21:20:56
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for t_student
-- ----------------------------
DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
`sid` int(11) NOT NULL,
`sname` varchar(10) NOT NULL,
`gender` varchar(2) NOT NULL,
`Classid` int(11) NOT NULL,
PRIMARY KEY (`sid`),
UNIQUE KEY `UK_SNAME` (`sname`),
KEY `UF_SCLASS` (`Classid`),
CONSTRAINT `UF_SCLASS` FOREIGN KEY (`Classid`) REFERENCES `t_class` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of t_student
-- ----------------------------
INSERT INTO `t_student` VALUES ('', '埼玉', '男', '');
INSERT INTO `t_student` VALUES ('', '李宏灿', '男', '');
INSERT INTO `t_student` VALUES ('', '娘口三三', '男', '');
INSERT INTO `t_student` VALUES ('', '路飞', '男', '');
INSERT INTO `t_student` VALUES ('', '齐博源', '男', '');
INSERT INTO `t_student` VALUES ('', '比较好', '男', '');
INSERT INTO `t_student` VALUES ('', '赵以浩', '男', '');
INSERT INTO `t_student` VALUES ('', '大雄', '男', '');

系统实现代码:

 package MySQL测试连接;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner; /**
*
* 创建时间:2019年12月2日 上午10:11:28
*
* 项目名称:MySQL_Test
*
* @author WYH
*
* @version 1.0
*
* @since JDK 1.8.0
*
* 文件名称:Test5.java
*
* 类说明:
* 对数据库操作的增删改查系统1.0版本
*/ public class Test5 {
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null; // 添加
public static void insertSql(String sql, Connection conn, int sid,
String sname, String gender, int Classid) {
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, sid);
ps.setString(2, sname);
ps.setString(3, gender);
ps.setInt(4, Classid);
int i = ps.executeUpdate();
System.out.println("有" + i + "记录被影响,成功添加一条数据!");
} catch (Exception e) {
System.out.println("添加失败!!");
} } // 查询
public static void selectSql(String SelectSql, Connection conn) {
try {
ps = conn.prepareStatement(SelectSql);
// ps.setString(1, "男");
rs = ps.executeQuery();
System.out.println("学号\t\t姓名\t性别\t所在班级");
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2)
+ "\t" + rs.getString(3) + "\t" + rs.getString(4));
}
} catch (Exception e) {
System.out.println("查询失败!!");
} } // 删除
public static void deleteSql(String DeleteSql, Connection conn, int sid1) {
try {
ps = conn.prepareStatement(DeleteSql);
ps.setInt(1, sid1);
int j = ps.executeUpdate();
System.out.println("有" + j + "条数据被影响,成功的删除" + sid1 + "数据");
} catch (Exception e) {
System.out.println("删除失败!!");
}
} // 更新
public static void updateSql(String UpdateSql, Connection conn,
String sname2, int sid2) {
try {
ps = conn.prepareStatement(UpdateSql);
ps.setString(1, sname2);
ps.setInt(2, sid2);
int i = ps.executeUpdate();
System.out.println("有" + i + "条数据被影响,成功更新数据。。");
} catch (Exception e) {
System.out.println("更新失败!!");
}
} public static void start() {
boolean flag = true;
Scanner sc = new Scanner(System.in);
System.out.println("===========欢迎使用t_student表的操作系统===========");
while (flag) {
System.out.println("1、添加数据\n2、查询数据\n3、更新数据\n4、删除数据\n5、退出");
System.out.print("请选择您需要的操作:");
int choice = sc.nextInt(); switch (choice) {
case 1:
// 添加数据
String sql = "insert into t_student(sid,sname,gender,Classid) values(?,?,?,?)";
System.out.print("请输入您要添加学号:");
int sid = sc.nextInt(); System.out.print("请输入您添加学生的姓名:");
String sname = sc.next(); System.out.print("请输入您添加学生的性别:");
String gender = sc.next(); System.out.print("请输入您添加学生的班级号:");
int Classid = sc.nextInt();
insertSql(sql, conn, sid, sname, gender, Classid); System.out.println("添加后的数据为:");
selectSql("select * from t_student", conn);
System.out.println();
System.out.println();
break; case 2:
// 查询数据
String SelectSql = "select * from t_student";
selectSql(SelectSql, conn);
System.out.println();
System.out.println();
break; case 3:
// 更新数据
String UpdateSql = "update t_student set sname=? where sid=? ";
System.out.print("请输入要更新姓名的学生学号:");
int sid2 = sc.nextInt(); System.out.print("请输入要更新该学号的新姓名:");
String sname2 = sc.next();
updateSql(UpdateSql, conn, sname2, sid2); System.out.println("更新后的数据为:");
selectSql("select * from t_student", conn);
System.out.println();
System.out.println();
break; case 4:
// 删除
String DeleteSql = "delete from t_student where sid=?";
System.out.print("请输入要删除的学生学号:");
int sid1 = sc.nextInt();
deleteSql(DeleteSql, conn, sid1); System.out.println("删除后的数据为:");
selectSql("select * from t_student", conn);
System.out.println();
System.out.println();
break;
case 5:
System.out.println("谢谢使用");
flag = false;
break;
default:
System.out.println("输入有误。。请重新输入");
System.out.println();
System.out.println();
break;
}
} } public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/school", "root", "root");
System.out.println(conn);
start(); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} } }

运行结果:

大数据之路week05--day01(JDBC 初识之实现一个系统 实现用户选择增删改查 未优化版本)

上一篇:java基础知识回顾之java Socket学习(二)--TCP协议编程


下一篇:9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】