用数据库作学生管理系统时问题的解决

来源:互联网 发布:人工智能计算器 安卓 编辑:程序博客网 时间:2024/05/17 07:44

1,首先遇到的是数据库显示与控制台录入不一致,解决办法是1,在mysql文件里找到my.ini安装目录,把里面的拉丁文改成utf8

[mysql]

default-character-set=utf8
[mysqld]

character-set-server=utf8

2,把文件的输出显示全换成utf8,统一格式,把异常率降到最低。

3.对遍历学生表,用到的PreparedStatement 与Statement 方法

PreparedStatement 方法可以预设?,方便写入参数如

String sql = "update student set name=?where id = ?";

PreparedStatement st = connect.prepareStatement(sql);

而Statement方法可以使用getInt()和getString()方法,返回响应的值,()既可以填列的索引值,也可以填列名如“ID”;

还有boolean isStudent = rs.next();方法,方便确认是否还有下一个值,用于查找学生;

public class Student extends Connect {
private Student(){

}
private static Student student;
public static Student getInstance(){
if(student == null){
student = new Student();
}
return student;
}
public  int loginData(){
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(ins);
int number = 0;
boolean flag = true;
while(flag){
try {


String num = buf.readLine();
number = Integer.parseInt(num);//数据类型转换
flag = false;
} catch (IOException e) {

System.out.println("I/o流输入异常");
}catch(NumberFormatException n){
System.out.println("输入数据类型错误,请从新输入");
}
}
return number;

}
public String writeIn(){
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(ins);
String str= "";
boolean flag = true;
while(flag){
try {
str = buf.readLine();
flag = false;
} catch (IOException e) {
System.out.println("I/o流输入异常");
}catch(NumberFormatException n){
System.out.println("输入数据类型错误,请从新输入");
}
}
return str;
}
public void addStudent(){
// loadDriver();
Connection connect = openConnection();
String sql = "insert into student(name,id,age,sex)values(?,?,?,?)";
PreparedStatement ps = null;
System.out.println("请输入学生姓名");
String name = writeIn();
System.out.println("请输入学生学号");
int id = loginData();
System.out.println("请输入学生年龄");
int age = loginData();
System.out.println("请输入学生性别");
String sex = writeIn();
try {
ps = connect.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, id);
ps.setInt(3, age);
ps.setString(4, sex);
ps.execute();
System.out.println("录入成功,请到数据库查看");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connect.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
public void updateStudent(){
// loadDriver();
Connection connect = openConnection();
String sql = "update student set name=?where id = ?";
PreparedStatement st = null;
System.out.println("请输入学生新的姓名");
String name = writeIn();
System.out.println("请输入该学生原有的学号");

int id = loginData();
try {
st = connect.prepareStatement(sql);
st.setString(1, name);
st.setInt(2,id);
st.execute();
System.out.println("学生"+name+"修改完成");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connect.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
public void findStudentById(){
// loadDriver();
Connection connect = openConnection();
String sql = "select*from student where id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
System.out.println("请输入要查找学生的学号");
int id = loginData();
try {
ps = connect.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();

boolean isStudent = rs.next();
System.out.println(isStudent==true?"找到学生":"没找到学生");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connect.close();
ps.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
public void deleteStudentById(){
// loadDriver();
Connection connect = openConnection();
Statement ps = null;
System.out.println("请输入要删除学生的学号");
int id = loginData();
try {
ps = connect.createStatement();
ps.execute("delete from student where id= "+id);
System.out.println("学号是"+id+"号的学生已删除");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
connect.close();
ps.close();

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

}
}

public void selectStudentAll(){
String sql = "select * from student";
Connection connect = openConnection();
try {
Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery(sql); //游标默认指向第一条记当上方
System.out.println("name"+"\t"+"id"+"\t"+"age"+"\t"+"sex");
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String sex = resultSet.getString("sex");
System.out.println(name+"\t"+id+ "\t"+age + "\t"+sex);
}

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


0 0