标签:port home private exec 变量 password 姓名 数据 tac
DBUtilTest数据库通用工具类
package homework;
import java.sql.*;
/**
* @ClassName: homework.DBUtilTest
* @Description: 数据库通用工具类
* @author: aweicy
* @date: 2020/4/25 12:04
*/
public class DBUtilTest {
//将数据库常用连接变量设置为常量
private static final String DRIVERCLASS="com.mysql.jdbc.Driver";
private static final String DB_NAME="info_student";
private static final String URL="jdbc:mysql://127.0.0.1:3306/"+DB_NAME;
private static final String JDBC_USERNAME="root";
private static final String JDBC_PASSWORD="gaowei123";
private static Connection conn;
private static PreparedStatement pst;
private static ResultSet rs;
private static int executeUpdate;
/**
* 获取数据库连接
* @return conn
*/
public static Connection getConnection(){
try {
Class.forName(DRIVERCLASS);
conn=DriverManager.getConnection(URL,JDBC_USERNAME,JDBC_PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//通用查询方法
public static ResultSet executeQuery(String sql,Object ...args){//可能有参数可能没有参数
//判断数据库是否连接
if(conn==null){
conn=getConnection();
}
try {
//提交sql
pst=conn.prepareStatement(sql);
//设置参数
setValue(pst,args);
//获取到结果集
rs=pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//通用的增删改
public static int executeUpdate(String sql,Object ...args){
//判断数据库是否连接
if(conn==null){
conn=getConnection();
}
try {
pst=conn.prepareStatement(sql);
//设置参数
setValue(pst,args);
executeUpdate=pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return executeUpdate;
}
//设置参数
public static void setValue(PreparedStatement pst,Object ...args){
//传参
if(args!=null&&args.length>0){
for (int i = 0; i < args.length; i++) {
try {
pst.setObject(i+1,args[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//关闭连接
public static void getClose(){
if(rs!=null){
try {
rs.close();
rs=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
pst=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
conn=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Menu菜单类
package homework;
/**
* @ClassName: Menu
* @Description: TODO
* @author: aweicy
* @date: 2020/4/30 20:29
*/
public class Menu {
public void showMainMenu(){
System.out.println("***********************************************************");
System.out.println("* 学生信息系统 *");
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("***********************************************************");
System.out.println("请选择你要进行的操作(请输入对应的编号):");
}
}
test类(主程序)
package homework;
import org.junit.Test;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
/**
* @ClassName: test
* @Description: TODO
* @author: aweicy
* @date: 2020/4/25 12:15
*/
public class test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Menu menu=new Menu();
String index;
while (true){
menu.showMainMenu();
index=in.next();
if(index.equals("1")){//添加功能
while(true){
System.out.println("请输入学生姓名:");
String sName=in.next();
System.out.println("请输入学生年龄:");
int sAge=in.nextInt();
Connection connection=DBUtilTest.getConnection();
String sql="INSERT INTO student(sid,sname,sage) VALUES (null,?,?)";
int executeUpdate=DBUtilTest.executeUpdate(sql,sName,sAge);
if(executeUpdate>0){
System.out.println("信息录入完成,确定(1),重新输入(2):");
index=in.next();
if(index.equals("1")){
System.out.println("数据添加成功,继续添加(1),返回首页(2):");
index=in.next();
if(index.equals("1")){
continue;
}else if(index.equals("2")){
DBUtilTest.getClose();
System.out.println("请选择:");
break;
}
}
}else {
System.out.println("信息录入失败");
DBUtilTest.getClose();
System.out.println("请选择:");
}
}
continue;
}else if(index.equals("2")){//修改功能
while (true){
System.out.println("请输入你要修改的学员编号:");
int sID=in.nextInt();
Connection connection=DBUtilTest.getConnection();
String sql="select * from student where sid=?";
ResultSet rs=DBUtilTest.executeQuery(sql,sID);
try {
System.out.println("编号\t\t姓名\t\t年龄");
while (rs.next()){
System.out.print(rs.getInt(1)+"\t\t");
System.out.print(rs.getString(2)+"\t\t");
System.out.println(rs.getInt(3));
}
}catch (SQLException e){
e.printStackTrace();
}
DBUtilTest.getClose();
System.out.println("请输入新姓名:");
String sName=in.next();
System.out.println("请输入新年龄:");
int sAge=in.nextInt();
connection=DBUtilTest.getConnection();
sql="Update student set sname=?,sage=? where sid="+sID;
int executeUpdate=DBUtilTest.executeUpdate(sql,sName,sAge);
if(executeUpdate>0){
System.out.println("修改数据已接收,确定修改(1),重新输入(2):");
index=in.next();
if(index.equals("1")){
System.out.println("数据修改成功,继续修改(1),返回首页(2):");
index=in.next();
if(index.equals("1")){
continue;
}else if(index.equals("2")){
DBUtilTest.getClose();
System.out.println("请选择:");
break;
}
}
}else {
System.out.println("数据修改失败");
DBUtilTest.getClose();
System.out.println("请选择:");
}
}
continue;
}else if(index.equals("3")){//查询功能
while (true){
System.out.println("请选择你要查询的方式 查询所有数据(1),根据编号查询(2)");
index=in.next();
if(index.equals("1")){
System.out.println("编号\t\t姓名\t\t年龄");
Connection connection=DBUtilTest.getConnection();
String sql="select * from student";
ResultSet rs=DBUtilTest.executeQuery(sql);
try {
while (rs.next()){
System.out.print(rs.getInt(1)+"\t\t");
System.out.print(rs.getString(2)+"\t\t");
System.out.println(rs.getInt(3));
}
}catch (SQLException e){
e.printStackTrace();
}
DBUtilTest.getClose();
}else if(index.equals("2")){
System.out.println("请输入你要查询的编号:");
int sID=in.nextInt();
Connection connection=DBUtilTest.getConnection();
String sql="select * from student where sid=?";
ResultSet rs=DBUtilTest.executeQuery(sql,sID);
try {
System.out.println("编号\t\t姓名\t\t年龄");
while (rs.next()){
System.out.print(rs.getInt(1)+"\t\t");
System.out.print(rs.getString(2)+"\t\t");
System.out.println(rs.getInt(3));
}
}catch (SQLException e){
e.printStackTrace();
}
DBUtilTest.getClose();
}
System.out.println("重新查询(1),返回首页(2)");
System.out.println("请选择:");
index=in.next();
if(index.equals("1")){
continue;
} else if(index.equals("2")){
break;
}
}
continue;
}else if(index.equals("4")){//删除功能
while (true){
System.out.println("请输入你要删除的编号:");
int sID=in.nextInt();
Connection connection=DBUtilTest.getConnection();
String sql="delete from student where sid="+sID;
int executeUpdate=DBUtilTest.executeUpdate(sql);
if(executeUpdate>0){
System.out.println("数据删除成功,继续删除(1),返回首页(2):");
index=in.next();
if(index.equals("1")){
continue;
}else if(index.equals("2")){
DBUtilTest.getClose();
System.out.println("请选择:");
break;
}
}else {
System.out.println("你输入的编号不存在,重新输入(1),返回首页(2)");
DBUtilTest.getClose();
System.out.println("请选择:");
index=in.next();
if(index.equals("1")){
continue;
}else if(index.equals("2")){
DBUtilTest.getClose();
System.out.println("请选择:");
break;
}
}
}
continue;
}
}
}
}
标签:port home private exec 变量 password 姓名 数据 tac
原文地址:https://www.cnblogs.com/aweicy-1/p/12811756.html