《学生信息管理系统 java 连接数据库》由会员分享,可在线阅读,更多相关《学生信息管理系统 java 连接数据库(5页珍藏版)》请在金锄头文库上搜索。
1、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.JdbcOdbcDriv
2、er;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,Connection
3、con=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()
4、;/查找该用户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);re
5、turn 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();
6、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 = co
7、nnection();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) / 查询学生publ
8、ic 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.
9、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 (SQL
10、Exception 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 statem
11、ent = 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;
12、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) Syste
13、m.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(请输入有效的操作数字);