资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Date;import java.util.Scanner;public class Student public static final String DRIVER=sun.jdbc.odbc.JdbcOdbcDriver;public static final String URL=jdbc:odbc:student;public static final String USERNAME=root;/佣户名public static final String PASSWORD=root;/密码private static String name;/当 前登录管理员的名字private static int userid;/当前登录管理员的主键获得jdbc链接public Connection connection()try Class.forName(DRIVER);USERNAME,Connectioncon=DriverManager.getConnection(URL,PASSWORD);return con; catch (Exception e) e.printStackTrace();return null;/管理员登陆public boolean login() throws ExceptionScanner sc = new Scanner(System.in);System.out.print(” 输入用户名:”);String username = sc.next();System.out.print(” 输入密码:);String password = sc.next();/查找该用户String sql = select username,id from muser where username= + username + and password= + password + ;Connection connection = connection();Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql);if (rs.next() name = rs.getString(1);userid = rs.getInt(2);return true;return false;/添加学生/添加学生public void addstudent()System.out.print(”请依次输入学号,姓名,年龄,地址(用逗号隔开):”);Scanner scanner = new Scanner(System.in);String str = scanner.next().split(,);/*begin 验证学号是否重复*/String sql = select * from student where 1=1 and stuno=+str0;try Connection connection = connection();Statement statement = connection.createStatement();ResultSet rs = statement.executeQuery(sql); if(rs.next()System.out.println(”该学号已经存在,请更换”);return; catch (SQLException ex) /*end 验证学号是否重复*/String sql1 = insert into student(stuno,stuname,stuage,stuaddress) values(?,?,?,?); Connection connection1 = connection();PreparedStatement statement1;try statement1 = connection1.prepareStatement(sql1);statement1.setString(1, str0); statement1.setString(2, str1);statement1.setString(3, str2); statement1.setString(4, str3);statement1.execute();System.out.println(” 添加学生成功!); catch (SQLException ex) / 查询学生public void checkstudent(String sno) throws SQLException System.out.println(n 学生列表:”);String sql = select * from student where 1=1 ; if (sno!=null & !.equals(sno) sql = sql + and stuno=+sno;System.out.println 学号+%+姓名+t+年龄+%+地址”); try Connection connection = connection();Statement statement = connection.createStatement();ResultSet rs = statement.executeQuery(sql); while(rs.next()/ int theid = rs.getInt(1); String sno1 = rs.getString(2); String name1 = rs.getString(3); String age1 = rs.getString(4); String address1 = rs.getString(5);/打印出来System.out.println(sno1+t+name1+t+age1+t+address1); catch (SQLException ex) System.out.println(nn);/删除学生public void deletestudent() System.out.print(”请输入要删除的学号:”);Scanner scanner = new Scanner(System.in);String stuno = scanner.next();String sql = delete from student where stuno=+stuno;Connection connection = connection();PreparedStatement statement; try statement = connection.prepareStatement(sql); statement.execute();System.out.println(” 删除学生成功!); catch (SQLException ex) / 主函数入口public static void main(String args) throws Exception Student theLibrary = new Student();/ 登陆boolean loginflag = true; while (loginflag) if(theLibrary.login()/ loginflag = false;else System.out.println(”用户名或者密码错误,请检查.); /登陆成功提示System.out.println(n你好,+ name +! 欢迎来到学生管理系统);boolean flag = true; while (flag) System.out.print(n操作说明:T添加学生,2删除学生,3查找学生,4显 示所有学生 ,-1 退出系统:);Scanner scanner = new Scanner(System.in);int input = 0;try input = scanner.nextInt(); catch (Exception e) System.out.println(请选择一个操作);continue;if (input=-1) System.out.println(” 谢谢使用,再见!);flag = false;else /添加学生if (input=1) theLibrary.addstudent();/删除学生else if (input=2) theLibrary.deletestudent();/查找学生else if (input=3) System.out.print(n 请输入要查找学生学号:);Scanner sc = new Scanner(System.in); theLibrary.checkstudent(sc.next();/查找学生else if (input=4) theLibrary.checkstudent();else System.out.println(请输入有效的操作数字);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号