澳门新萄京官方网站-www.8455.com-澳门新萄京赌场网址

澳门新萄京官方网站增删改查,电话薄设计

2019-09-29 作者:数据库网络   |   浏览(113)

这里使用JAVA语言编写的简易的学生选课系统,展现的都是这个系统核心代码。

package com.hanqi.telbook;

package com.db.conn;

1.方法类

1.方法类

其中有不足欢迎批评和指正!

import java.util.Scanner;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

package com.com;

package com.com;

 

public class Menu {
//主菜单

public class ConOracl {

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

链接数据库的代码

public void mainMenu()
{

// 数据库驱动类
private String dbDriver = "oracle.jdbc.driver.OracleDriver";
// 连接数据库url
private String dbURL = "jdbc:oracle:thin:@localhost:1521:xe";
// 连接数据库用户名
private String dbUser = "system";
// 连接数据库密码
private String dbPwd = "123";
// 获取数据库连接方法, 返回Connection对象
private Connection con = null;
//数据执行语句
private Statement  stat = null;
private String sql=null;
private ResultSet rs=null;
//创建数据库连接
public Connection getDBConnect() {
try {
// 加载数据库驱动
Class.forName(dbDriver);
con = DriverManager.getConnection(dbURL, dbUser, dbPwd);
} catch (Exception e) {
System.out.println(e);
}
return con;
}
//增加
public void Add()
{
con=getDBConnect();
sql="insert into student(stuno,stuname,stusex)"
"values('1','lucy','w')";
try {
stat=con.createStatement();
stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除
public void Delete()
{
con=getDBConnect();
sql="delete  from student "
"where stuno=1";
try {
stat=con.createStatement();
stat.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

 

 

package connection;
//连接数据库student

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("** 6 退出记录 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:6");

}
//修改
public void Update()
{
con=getDBConnect();
sql="update student set stuname='ben' where stuno='2'";
try {
stat=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询
public void Search() {
con = getDBConnect();
// 创建表的sql语句
sql = "SELECT * FROM student";
try {
stat = con.createStatement();
rs= stat.executeQuery(sql);
while (rs.next()) {
String stuno = rs.getString(1);
String stuname = rs.getString(2);
String stusex = rs.getString(3);
System.out.println(stuno "," stuname "," stusex);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//功能:关闭数据库的连接
 
    public void close() {//6.释放资源
      try { // 捕捉异常
           try {
               if (rs!= null) { // 当ResultSet对象的实例rs不为空时
                rs.close(); // 关闭ResultSet对象
              }
            } finally {
              try {
                    if (stat != null) { // 当Statement对象的实例stmt不为空时
                    stat.close(); // 关闭Statement对象
                    }
              } finally {
                   if (con!= null) { // 当Connection对象的实例conn不为空时
                        con.close(); // 关闭Connection对象
                }
              }
         }
       } catch (Exception e) {
           e.printStackTrace(System.err); // 输出异常信息
       }
   }
public static void main(String[] args) {
ConOracl oracl = new ConOracl();
System.out.println("操作之前:");
oracl.Search();
oracl.Add();
oracl.Delete();
oracl.Update();
System.out.println("操作之后:");
oracl.Search(); 
oracl.close();
}
}

public class tool {

public class tool {

import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.Statement;
import java.sql.SQLException;

}
//添加用户菜单
public void addMenu()
{

/**
* @throws Exception
*/
static Connection conn =null;
static Statement st =null;
static ResultSet rs =null;
public static void mains() {

/**
* @throws Exception
*/
static Connection conn =null;
static Statement st =null;
static ResultSet rs =null;
public static void mains() {

public class xu{

System.out.println("**************************");
System.out.println("** 1 添加新记录 **");
System.out.println("** 2 查看全记录 **");
System.out.println("** 3 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:3");

try{
Class.forName("oracle.jdbc.OracleDriver");
conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","mysql","123456");
st=conn.createStatement();
rs=st.executeQuery("select numno,ename from dept");

try{
Class.forName("oracle.jdbc.OracleDriver");
conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","mysql","123456");
st=conn.createStatement();
rs=st.executeQuery("select numno,ename from dept");

private static Connection conn = null;
static {
try {
// 注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获得一个数据库连接
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=student","sa","123456");
}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
//查找用户菜单
public int searchMenu()
{int rst=0;
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("** 6 查看全记录 **");
System.out.println("** 7 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:7");

while (rs.next()) {
System.out.println(rs.getInt("numno") "---" rs.getString("ename"));
}

while (rs.next()) {
System.out.println(rs.getInt("numno") "---" rs.getString("ename"));
}

public static Connection getConnection() {
return conn;
}
}

Scanner sc=new Scanner(System.in );

}catch (Exception e) {
e.printStackTrace();
}/*finally {
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
}
//添加数据
public static void add(String num1, String name1) throws SQLException{
int addRe=st.executeUpdate("insert into dept (numno,ename) values (" num1 ",'" name1 "')");
System.out.println("******添加操作结果是:" addRe);

}catch (Exception e) {
e.printStackTrace();
}/*finally {
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
}
//添加数据
public static void add(String num1, String name1) throws SQLException{
int addRe=st.executeUpdate("insert into dept (numno,ename) values (" num1 ",'" name1 "')");
System.out.println("******添加操作结果是:" addRe);

 

while(true)
{ try{
rst=sc.nextInt();
if(rst>=1&&rst<=7)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

}
//删除数据
public static void delete(String num2) throws SQLException{
int dele=st.executeUpdate("delete from dept where numno=" num2 "");
System.out.println("******删除操作结果是:" dele);
}
//修改数据
public static void alters(String num3, String name2) throws SQLException{
int alt=st.executeUpdate("update dept set ename='" name2 "'where numno=" num3 "");
System.out.println("******删除操作结果是:" alt);

}
//删除数据
public static void delete(String num2) throws SQLException{
int dele=st.executeUpdate("delete from dept where numno=" num2 "");
System.out.println("******删除操作结果是:" dele);
}
//修改数据
public static void alters(String num3, String name2) throws SQLException{
int alt=st.executeUpdate("update dept set ename='" name2 "'where numno=" num3 "");
System.out.println("******删除操作结果是:" alt);

 

catch(Exception e)
{
System.out.println("输入不合法,请重新输入:");
}
break;
}
return rst;
}
//修改用户信息主菜单
public void modifyMenu()
{
System.out.println("**************************");
System.out.println("** 1 查看全记录 **");
System.out.println("** 2 修改指定记录 **");
System.out.println("** 3 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:3");
}
//修改用户信息子菜单
public int subModifyMenu()
{
int rst=0;
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("** 6 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:6");
Scanner sc=new Scanner(System.in );
while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=6)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

}

}

选课的操作代码

return rst;

}

}

package connection;

}
//删除用户信息菜单
public int deleteMenu()
{
int rst=0;
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("请输入正确的数字,最小是:1,最大是:4");
Scanner sc=new Scanner(System.in );

 

 

import java.sql.*;
import java.util.*;

while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=4)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

2.调用类(执行类,测试类)

2.调用类(执行类,测试类)

public class test1{

return rst;
}
//排序用户信息菜单
public int orderMenu()
{
int rst=0;
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("请输入正确的数字,最小是:1,最大是:5");
Scanner sc=new Scanner(System.in );

import java.sql.SQLException;
import java.util.Scanner;

import java.sql.SQLException;
import java.util.Scanner;

private static String ID; // 此处的ID设为全局变量,在下面某些方法里会用到
private static Scanner console = new Scanner(System.in);
private static String managerPass = "123456";
private static Connection conn = xu.getConnection();

while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=5)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

public class test {

public class test {

public static void main(String [] args) throws SQLException {

return rst;
}

/**
* @param args
*/
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println(" 1.添加 2.删除 3.修改 4.退出 ");
int num = 0;

/**
* @param args
*/
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println(" 1.添加 2.删除 3.修改 4.退出 ");
int num = 0;

System.out.println("Welcome to the student selection system!nn"
"if you are maneger please choose 6、7、8、9、10n"
"if you are student Please choose 1、2、3、4、5、10n");
ShowMenu();
}

public void start() throws Exception
{
while(true)
{
TelNoteRegex tnr=new TelNoteRegex();
Operate op=new Operate();
int i=tnr.menuRegex(1, 6);
switch(i)
{//添加
case 1:
addMenu();
tnr.addmenu(1, 3);
break;
//查找
case 2:

try {
num = sca.nextInt();
} catch (Exception e) {
System.out.println("异常");
break;
}

try {
num = sca.nextInt();
} catch (Exception e) {
System.out.println("异常");
break;
}

public static void ShowMenu() throws SQLException {

op.searchLogic();
break;
//修改
case 3:

switch (num) {
case 1:
System.out.println("添加前的数据");
tool.mains();
System.out.println("请输入您添加的编号:");
String num1 = sca.next();
//保证输入的是数字
for (int i = num1.length(); --i >= 0;) {
int nums = num1.charAt(i);
if (num1.length() == 4 && nums >= 48 && nums <= 57) {

switch (num) {
case 1:
System.out.println("添加前的数据");
tool.mains();
System.out.println("请输入您添加的编号:");
String num1 = sca.next();
//保证输入的是数字
for (int i = num1.length(); --i >= 0;) {
int nums = num1.charAt(i);
if (num1.length() == 4 && nums >= 48 && nums <= 57) {

System.out.println("-----------option-------------n");

op.modifyLogicLogic();
break;
//删除
case 4:

System.out.println("请输入您添加的名称:");
String name1 = sca.next();

System.out.println("请输入您添加的名称:");
String name1 = sca.next();

System.out.println("1--Student login"); // 学生登录
System.out.println("2--Change password"); // 学生修改密码
System.out.println("3--Choose course"); // 学生选课
System.out.println("4--Get course list"); // 学生查看自己的课程表
System.out.println("5--Student rigistration"); // 学生注册账号
System.out.println("6--Create student"); // 管理员创建学生
System.out.println("7--Create course"); // 管理员创建课表
System.out.println("8--Get student list"); // 管理员查看学生表
System.out.println("9--Get course list1"); // 管理员查看选课表里的课程
System.out.println("10--exitn"); // 退出

op.deleteLogic();
break;
//排序
case 5:
op.orderLogic();
break;
//退出
case 6:

try {
tool.add(num1, name1);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("添加后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}

try {
tool.add(num1, name1);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("添加后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}

Test();
}

break;

break;
//删除数据
case 2:
System.out.println("删除前的数据");
tool.mains();
System.out.println("请输入您删除的编号:");
String num2 = sca.next();
//数字
for (int i = num2.length(); --i >= 0;) {
int nums1 = num2.charAt(i);
if (num2.length() == 4 && nums1 >= 48 && nums1 <= 57) {
try {
tool.delete(num2);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("删除后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}

break;
//删除数据
case 2:
System.out.println("删除前的数据");
tool.mains();
System.out.println("请输入您删除的编号:");
String num2 = sca.next();
//数字
for (int i = num2.length(); --i >= 0;) {
int nums1 = num2.charAt(i);
if (num2.length() == 4 && nums1 >= 48 && nums1 <= 57) {
try {
tool.delete(num2);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("删除后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}

public static void Test() throws SQLException {

default:
break;

break;
case 3:
//修改数据
System.out.println("修改前的数据");
tool.mains();
System.out.println("请输入您修改的编号:");

break;
case 3:
//修改数据
System.out.println("修改前的数据");
tool.mains();
System.out.println("请输入您修改的编号:");

System.out.println("manager choose 1 ,student choose 0");
int choose1 = console.nextInt(); //int 用nextInt()

}
if(i==6)

String num3 = sca.next();
//数字
for (int i = num3.length(); --i >= 0;) {
int nums2 = num3.charAt(i);
if (num3.length() == 4 && nums2 >= 48 && nums2 <= 57) {
System.out.println("请输入您修改的名称:");
String name2 = sca.next();
try {
tool.alters(num3, name2);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("修改后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}

String num3 = sca.next();
//数字
for (int i = num3.length(); --i >= 0;) {
int nums2 = num3.charAt(i);
if (num3.length() == 4 && nums2 >= 48 && nums2 <= 57) {
System.out.println("请输入您修改的名称:");
String name2 = sca.next();
try {
tool.alters(num3, name2);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("修改后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}

if(choose1 == 1) {

break;

break;
case 4:
System.out.println("您已退出系统!");
flag = false;
break;

break;
case 4:
System.out.println("您已退出系统!");
flag = false;
break;

System.out.println("Please input your password :");
String password = console.next(); //String 用next()

}
}
}

default:
System.out.println("请输入规定的数字!");
break;
}

default:
System.out.println("请输入规定的数字!");
break;
}

if(password.equals(managerPass)) {

package com.hanqi.telbook;
import java.util.Scanner;

}

}

System.out.println("Welcome manager!n");
ManagerMenu1();
}

public class TelNoteRegex {
//对菜单输入选项的验证

}

}

else {

public int menuRegex (int min, int max )
{
int scc=0;
try{
Menu me=new Menu();

}

}

System.out.println("Your password is worry!"); }
}

Scanner sc =new Scanner(System.in);
while(true)
{
me.mainMenu();
int i=sc.nextInt();
if(i>=min&&i<=max&&i%1==0)
{
scc=i;
break;
}
}
}

else if(choose1 == 0) {

catch(Exception e)
{
System.out.println("输入的数字不合法");

System.out.println("Welcome Student!");
StudentMenu1();
}
}

}
return scc;
}
//添加用户的子菜单
public int addmenu(int min,int max)throws Exception
{
int scc=0;
try{
TelNoteRegex tnr=new TelNoteRegex();
Operate op=new Operate();
Scanner sc =new Scanner(System.in);
while(true)
{

/*管理员操作菜单*/
public static void ManagerMenu1() throws SQLException {

int i=sc.nextInt();

System.out.println("----------Manager----------n");
System.out.println("Please choose option");
int choose2 = console.nextInt();

if(i>=min&&i<=max&&i%1==0)
{
scc=i;
switch(i)
{
case 1:
op.addOperation();
break;
case 2:
op.showAll();

switch(choose2) {

break;
case 3:
Menu mee=new Menu();
mee.start();
break;
default:
break;
}

case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
System.out.println("This is student`s option,try again!");
ManagerMenu1();
break;
case 6 : CreateStu();break;
case 7 : CreateCourse();break;
case 8 : GetStudentList();break;
case 9 : GetCourseList1();break;
case 10 : System.exit(0);break;
default : System.out.println("Please input an right number , try again!"); ManagerMenu1();

return scc;

}
}

}
break;
}
}
catch(Exception e)
{
System.out.println("输入的数字不合法");

/*学生操作菜单*/
public static void StudentMenu1() throws SQLException {

}
return scc;
}
//对用户输入姓名的验证
public String nameRegex()
{
String name="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入姓名:1-10位字母");
String str=sc.nextLine();
if(str.matches("[a-zA-Z]{1,10}"))
{
name=str;
break;
}
}
return name;
}
//对用户输入年龄的验证
public int ageRegex()
{
int age=0;
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入年龄,1-100 ");
int ii=sc.nextInt();
if(ii>0&&ii<=100)
{
age=ii;
break;
}
}
return age;
}
// 对用户输入性别的验证
public String sexRegex()
{
String sex="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入性别,(男 m or M)(女 f or F)");
String str=sc.nextLine();
if(str.equals("男")||str.equals("m")||str.equals("M")
||str.equals("女")||str.equals("f")||str.equals("F"))
{
sex=str;
break;

System.out.println("----------Student----------n");
System.out.println("Please input option");
int choose3 = console.nextInt();

}
}
return sex;
}
//对用户输入电话号码的验证
public String telNumRegex()
{
String tel="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入电话号码,6-10位数字");
String tell=sc.nextLine();
if(tell.matches("[0-9]{6,10}"))
{
tel=tell;
break;
}
}
return tel;
}
//对用户输入地址的验证
public String addressRegex()
{
String add="";

switch(choose3) {

while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入地址,1-50位字母或数字");
String str=sc.nextLine();
if(str.matches("[A-Za-z0-9]{1,50}"))
{
add=str;
break;
}
}
return add;
}

case 1 : StuLogin();break;
case 2 : ChangePass();break;

}

//在进行Choose course,Get course list之前都要求学生进行StuLogin操作

package com.hanqi.telbook;

case 3 :
case 4 : System.out.println("you must login before you can do it!n");StuLogin();break;
case 5 : StuRigistration();break;
case 6 :
case 7 :
case 8 :
case 9 : System.out.println("This is manager`s option,try again!");StudentMenu1();break;
case 10 : System.exit(0);break;
default : System.out.println("Please input an right number,try again!");StudentMenu1();

public class Person {
private int id;
private String name;
private int age;
private String sex;
private String telnum;
private String address;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelnum() {
return telnum;
澳门新萄京官方网站,}
public void setTelnum(String telnum) {
this.telnum = telnum;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}
}

public Person(String name, int age, String sex, String telnum, String address) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.telnum = telnum;
this.address = address;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [序号=" id ", 姓名=" name ", 年龄=" age ", 性别=" sex ", 电话号码=" telnum

/*管理员创建课程*/
public static void CreateCourse() throws SQLException{

  • ", 地址=" address "]";
    }

try {
/* 通过connection数据库链接对象 创建一个statement对象数据库操作对象,stat对象再获取一个执行sql的语句。
* stat对象就是java程序与Database的一个连接通道*/

}

Statement stat = conn.createStatement();
String sql1 = "insert into course(couID,couName,couTeacher)values(1,'软件工程','黄伟国')";
String sql2 = "insert into course(couID,couName,couTeacher)values(2,'数据库','韦美雁')";
String sql3 = "insert into course(couID,couName,couTeacher)values(3,'算法设计','黎明')";
String sql4 = "insert into course(couID,couName,couTeacher)values(4,'大学英语','章琴')";
String sql5 = "insert into course(couID,couName,couTeacher)values(5,'Java','唐雅媛')";

package com.hanqi.telbook;

int a = stat.executeUpdate(sql1);
int b = stat.executeUpdate(sql2);
int c = stat.executeUpdate(sql3);
int d = stat.executeUpdate(sql4);
int e = stat.executeUpdate(sql5);

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.mchange.v2.c3p0.ComboPooledDataSource;

int sum = a b c d e;
System.out.println("成功添加" sum "行课程数据");
ManagerMenu1();

public class Operate {

} catch (SQLException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("Course already exists");

ComboPooledDataSource cp=new ComboPooledDataSource("helloc3p0");
//list 集合
private List<Person> ls= new ArrayList<>();

ManagerMenu1();
}
}

//用户添加信息业务逻辑控制
public void addLogic()
{
TelNoteRegex tnr=new TelNoteRegex();
//配置连接池
try {

/*管理员创建学生*/
public static void CreateStu() throws SQLException {

Connection conn=cp.getConnection();
String sql="insert into person values(sq_person.nextval,?,?,?,?,?)";
PreparedStatement ps= conn.prepareStatement(sql);

try {
Statement stat = conn.createStatement();
String sql1 = "insert into student(stuID,stuName) Values(20160501,'徐明正')";

ps.setString(1, tnr.nameRegex());
ps.setInt(2, tnr.ageRegex());
ps.setString(3, tnr.sexRegex());
ps.setString(4, tnr.telNumRegex());
ps.setString(5, tnr.addressRegex());
ps.executeUpdate();
System.out.println("添加账户成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//用户查询信息业务逻辑控制
public List<Person> searchLogic() throws Exception
{
Menu me=new Menu();
List<Person> p=null;
switch(me.searchMenu())
{
case 1:
searchByName();

// 这里对学生信息的输入也必须一个一个输入,这是系统的缺陷,问题待解决

break;
case 2:
searchByAge();

int a = stat.executeUpdate(sql1);
System.out.println("成功添加" a "行学生数据");
ManagerMenu1();

break;
case 3:
searchBySex();

}catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("The student already exists");

break;
case 4:
searchByTelNum();

ManagerMenu1();
}
}

break;
case 5:
searchByAdd();

/*学生登录*/
public static void StuLogin() throws SQLException {

break;
case 6:
showAll();

System.out.println("Please input your stuID"); // 输入账号
ID = console.next();

break;
case 7:

System.out.println("Please input your Password"); // 输入密码
String stuPassword = console.next();

break;

String stuID = null;
String Password = null;

default:
break;
}

Statement stat ;
ResultSet rs ;

return p;
}

try {
stat = conn.createStatement();
String sql1 = "select * from login "; /* 从login表中查询其所有属性值,则stuID 和 Password都有了。
rs是结果集。查询出的记录是一个列表,初始时指针指向的是第一条记录之前的。*/
boolean flag;
rs = stat.executeQuery(sql1);

//修改信息业务逻辑控制
public void modifyLogicLogic() throws Exception
{
try{
Menu me=new Menu();
me.modifyMenu();
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1)
{
showAll();
}
else if(i==2)
{Menu mee=new Menu();

while(flag = rs.next()) { // rs.next(); //返回值为true or false

switch(mee.subModifyMenu())
{
case 1:

stuID = rs.getString("stuID");
Password = rs.getString("Password");

Scanner sc2=new Scanner(System.in);

if( ID.equals(stuID) && stuPassword.equals(Password)) // 注意这里,跳出循环的条件很重要

System.out.println("请输入需要修改的ID号");
int id=sc2.nextInt();
modifyname( id);
break;
case 2:

break;

Scanner sc3=new Scanner(System.in);

// 如果在数据库里没有找到与用户输入的stuID和Password相匹配的stuID和Password,flag则变为false

System.out.println("请输入需要修改的ID号");
int id1=sc3.nextInt();
modifyage( id1);
break;
case 3:

}

Scanner sc4=new Scanner(System.in);

if(!flag){ // 当flag 为 false 时

System.out.println("请输入需要修改的ID号");
int id2=sc4.nextInt();

System.out.println("The ID or Password is worry!Please input your ID and Password againn");
StuLogin();
}

modifysex(id2);
break;
case 4:

else {
System.out.println("***" ID "***" stuID "***" stuPassword "***" Password "***");

Scanner sc5=new Scanner(System.in);

System.out.println("Login Successful!n");
System.out.println("------------------Please choose option------------------n"
// "If you want to Change Password ,Please choose 2n"
"If you want to Choose Course,Please choose 3n" "If you want to Get Course List,Please choose 4n"
"If you want to exit ,Please choose 8n");

System.out.println("请输入需要修改的ID号");
int id3=sc5.nextInt();
modifytelnum( id3);
break;
case 5:

int choose4 = console.nextInt();

Scanner sc6=new Scanner(System.in);

switch(choose4) {

System.out.println("请输入需要修改的ID号");
int id4=sc6.nextInt();
modifyaddress( id4);
break;
case 6:
modifyLogicLogic();
break;

// case 2 : ChangePass();break;
case 3 : ChooseCourse();break;
case 4 : GetCourseList();break;
case 10 : System.exit(0);break;
default : System.out.println("You input an invalid number!");

}
}

}

else if(i==3)
{

}

me.start();

} catch (SQLException e) {

}
}
catch(Exception e)
{
System.out.println("输入不合法,请重新输入!");
}
}

// TODO Auto-generated catch block
e.printStackTrace();
}

//删除信息业务逻辑控制
public void deleteLogic() throws Exception
{
Menu menu=new Menu();
while(true)
{

}

switch(menu.deleteMenu())
{
case 1:
showAll();

/*在这里说明一下 方法execute 、executeQuery、executeUpdate三者的区别
* 方法executeQuery 用于单个结果集的语句,如select。他会把查询的结果放入ResultSet类对象中供使用。
* 方法executeUpdate 用于执行insert、update或delete语句<返回值为整数>以及SQL DDL(数据定义语言),如create table 和 drop table <返回值为零>。
* 方法execute 用于执行返回多个结果集、多个更新计数或二者组合的语句。*/

break;
case 2:
delete();
break;
case 3:
deleteAll();
break;
case 4:

/*学生修改密码*/
public static void ChangePass() throws SQLException {

menu.start();
break;
default:
System.out.println("输入不正确");
break;
}

// System.out.println(ID);

}
}
//排序信息业务逻辑控制
public List<Person> orderLogic() throws Exception
{

/*此处的ID应该必须要重新输入,否则当选择对数据库中某个ID更新的Password进行修改时,没有对应的ID*/

Menu menu=new Menu();
while(true)
{
switch(menu.orderMenu())
{
case 1:
orderName();

System.out.println("Please input your ID");
String ID = console.next();
System.out.println("Please input your new Password");
String NewPassword = console.next();

break;
case 2:
orderAge();

Statement stat ;

break;
case 3:
orderSex();

try {

break;
case 4:
showAll();

stat = conn.createStatement(); // 两种写法都可以
// stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql ="UPDATE login SET Password =" "" NewPassword " where stuID =" ID;

break;
case 5:
menu.start();
break;
default:
System.out.println("输入错误");
break;
}
}
}
//添加新用户信息
public void addOperation()
{
Operate or= new Operate();
or.addLogic();
}
//查询全部用户信息
public List<Person> showAll()
{
try {
Connection conn= cp.getConnection();

//注意,此处的NewPassword 和 ID 没有要用''括起来

Statement st=conn.createStatement();
String sql="select * from person";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

// String sql1 ="UPDATE login SET Password = 111 where stuID = 20156022";

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}

int a = stat.executeUpdate(sql);
System.out.println(a);

rs.close();
st.close();
conn.close();

if(a != 0) {

} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按姓名查询用户信息
public List<Person> searchByName()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where name=?";
try {
Connection conn= cp.getConnection();

System.out.println("成功修改" a "行记录");
System.out.println("Your new Password is " NewPassword);

PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, tnr.nameRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集

StudentMenu1();
}

while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

else System.out.println("n" "Changing the Password is failed!");

ls.add(bc);
}

}catch(SQLException e) {

for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();

// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;

}

}
//按年龄查询用户信息
public List<Person> searchByAge()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where age=?";
try {
Connection conn= cp.getConnection();

/*学生选课*/
public static void ChooseCourse() throws SQLException {

PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, tnr.ageRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

System.out.println(ID);
String couID = null;
String couName = null;
String couTeacher = null;

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按性别查询用户信息
public List<Person> searchBySex()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where sex=?";
try {
Connection conn= cp.getConnection();

Statement stat;
ResultSet rs;

PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, tnr.sexRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

try {

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按电话号码查询用户信息
public List<Person> searchByTelNum()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where telnum=?";
try {
Connection conn= cp.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,tnr.telNumRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

stat = conn.createStatement();
String sql = "select * from course";
rs = stat.executeQuery(sql);

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按地址查询用户信息
public List<Person> searchByAdd()
{
TelNoteRegex tnr=new TelNoteRegex();
String sql="select * from person where address=?";
try {
Connection conn= cp.getConnection();

System.out.println("--------可选课程的信息-------");
while(rs.next()) {

PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, tnr.addressRegex());
ResultSet rs=ps.executeQuery();
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

couID = rs.getString("couID");
couName = rs.getString("couName");
couTeacher = rs.getString("couTeacher");

ls.add(bc);
}
for(Person p1:ls)
{
System.out.println(p1);
}
if(!rs.next())
{
System.out.println("数据库没有相关资料");
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//修改指定名字信息
public void modifyname(int id )
{
try {

System.out.println(couID " " couName " " couTeacher);
}

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set name=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.nameRegex());
ps.setInt(2, id);
ps.executeUpdate();
System.out.println("姓名修改成功");
ps.close();
conn.close();
cp.close();

System.out.println("n Please choose your course n");

} catch (Exception e) {

stat= conn.createStatement();

System.out.println("姓名修改不成功");
}
}
//修改指定年龄信息
public void modifyage(int id1 )
{
try {

// 此处stuID固定位学生登录时所用的ID,这样可以保证登录自己的账号却能选别的账号的课程

Connection conn=cp.getConnection();

System.out.println("input CourseID");
int ID1 = console.nextInt();
int ID2 = console.nextInt();
int ID3 = console.nextInt();

TelNoteRegex tnr=new TelNoteRegex();

// 这样写 ,学生选课的数目必须固定为3门,问题待解决

String sql="update person set age=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setInt(1, tnr.ageRegex());
ps.setInt(2, id1);
ps.executeUpdate();
System.out.println("年龄修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {

String sql1 = "insert into stuCourse(stuID,couID1,couID2,couID3)VALUES("

System.out.println("年龄修改不成功");
}
}
//修改指定性别信息
public void modifysex(int id2 )
{
try {

  • ID "," ID1 "," ID2 "," ID3 ")";
    int i = stat.executeUpdate(sql1);

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set sex=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.sexRegex());
ps.setInt(2, id2);
ps.executeUpdate();
System.out.println("性别修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("性别修改不成功");
}
}
//修改指定号码信息
public void modifytelnum(int id3 )
{
try {

if(i != 0) {
System.out.println("成功选入课程");

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set telnum=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.telNumRegex());
ps.setInt(2, id3);
ps.executeUpdate();
System.out.println("电话号码修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("电话号码修改不成功");
}
}
//修改指定地址信息
public void modifyaddress(int id4 )
{
try {

GetCourseList();
}
else System.out.println("选课错误");

Connection conn=cp.getConnection();
TelNoteRegex tnr=new TelNoteRegex();
String sql="update person set address=?where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, tnr.addressRegex());
ps.setInt(2, id4);
ps.executeUpdate();
System.out.println("地址修改成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("地址修改不成功");
}
}
//删除指定用户信息
public void delete()
{
try {
Connection conn=cp.getConnection();
Scanner scc=new Scanner(System.in);
System.out.println("输入需要删除的id");
int id=scc.nextInt();
String sql="delete person where id=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
System.out.println("删除成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("删除不成功");
}
}
//删除全部用户信息
public void deleteAll()
{
try {
Connection conn=cp.getConnection();
String sql="delete person ";
PreparedStatement ps= conn.prepareStatement(sql);
ps.executeUpdate();
System.out.println("删除成功");
ps.close();
conn.close();
cp.close();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("删除不成功");
}
}
//按用户姓名排序信息
public List<Person> orderName()
{
try {
Connection conn= cp.getConnection();
Statement st=conn.createStatement();
String sql="select * from person order by name ";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));

/*学生获取已选课程列表*/
public static void GetCourseList() throws SQLException {

ls.add(bc);
}
for(Person p:ls)
{
System.out.println(p);
}

System.out.println("nHere is your course that you have choosen");

rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按用户年龄排序信息
public List<Person> orderAge()
{
try {
Connection conn= cp.getConnection();
Statement st=conn.createStatement();
String sql="select * from person order by age ";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));
ls.add(bc);
}
for(Person p:ls)
{
System.out.println(p);
}
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}
//按用户性别排序信息
public List<Person> orderSex()
{
try {
Connection conn= cp.getConnection();
Statement st=conn.createStatement();
String sql="select * from person order by sex ";
ResultSet rs=st.executeQuery(sql);
//遍历结果集
while(rs.next())
{
Person bc=new Person();
bc.setId(rs.getInt("id"));
bc.setName(rs.getString("name"));
bc.setAge(rs.getInt("age"));
bc.setSex(rs.getString("sex"));
bc.setTelnum(rs.getString("telnum"));
bc.setAddress(rs.getString("address"));
ls.add(bc);
}
for(Person p:ls)
{
System.out.println(p);
}
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return ls;
}

Statement stat = null;
ResultSet rs = null;

}

try {

package com.hanqi.telbook;

import java.util.List;

public class App {

    public static void main(String[] args) {
        Menu me=new Menu();
        me.start();




    }

}

stat = conn.createStatement();
String sql = "select * from stuCourse ";
rs = stat.executeQuery(sql);

 

String stuID = null;
String couID1 = null;
String couID2 = null;
String couID3 = null;
String couID4 = null;

while(rs.next()) {

stuID = rs.getString("stuID");
couID1 = rs.getString("couID1");
couID2 = rs.getString("couID2");
couID3 = rs.getString("couID3");
couID4 = rs.getString("couID4");

if(stuID.equals(ID))

System.out.println(stuID " " couID1 " " couID2 " " couID3 " " couID4 "n");

}

}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/*学生注册*/
public static void StuRigistration() throws SQLException{

System.out.println("Please input your ID ");
String ID = console.next();
System.out.println("Please input your Password");
String Password = console.next();

try {
Statement stat = conn.createStatement();
String sql1 = "insert into login(stuID,Password)Values(" ID ", " Password ")";
//这里密码只能输入数字,问题待解决

int a = stat .executeUpdate(sql1);
System.out.println("成功注册" a "个账号");

StudentMenu1();

}catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("ID already exists");
}
}

/* 管理员查看学生表 */
public static void GetStudentList() {

System.out.println("n Here is student` information n");

Statement stat = null;
ResultSet rs = null;

try {

stat = conn.createStatement();
String sql = "select * from student ";
rs = stat.executeQuery(sql);

String stuID = null;
String stuName = null;

while(rs.next()) {

stuID = rs.getString("stuID");
stuName= rs.getString("stuName");

System.out.println(stuID " " stuName "n");

ManagerMenu1();
}

}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/* 管理员查看选课表里的课程*/
public static void GetCourseList1() {

System.out.println("n Here is course`information n");

Statement stat = null;
ResultSet rs = null;

try {

stat = conn.createStatement();
String sql = "select * from course ";
rs = stat.executeQuery(sql);

String couID = null;
String couName = null;
String couTeacher = null;

while(rs.next()) {

couID = rs.getString("couID");
couName = rs.getString("couName");
couTeacher = rs.getString("couTeacher");

System.out.println(couID " " couName " " couTeacher "n");

ManagerMenu1();
}

}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

本文由澳门新萄京官方网站发布于数据库网络,转载请注明出处:澳门新萄京官方网站增删改查,电话薄设计

关键词: