学生信息管理系统 java 连接数据库

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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);

Connection con=DriverManager.getConnection(URL, USERNAME, PASSWORD);

return con;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

//管理员登陆

public boolean login() throws Exception{

Scanner 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="+str[0];

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, str[0]);

statement1.setString(2, str[1]);

statement1.setString(3, str[2]);

statement1.setString(4, str[3]);

相关文档
最新文档