用数据库作学生管理系统时问题的解决
来源:互联网 发布:人工智能计算器 安卓 编辑:程序博客网 时间: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();
}
}
}
}
- 用数据库作学生管理系统时问题的解决
- 数据库—学生管理系统问题
- 数据库的搭建----学生管理系统心得体会
- 基于数据库的学生管理系统
- 学生管理系统中遇到的问题
- 学生管理系统----学生连接数据库类
- 学生成绩数据库管理系统
- 学生管理系统-出现问题
- 学生管理系统-基于SQLite数据库的Android应用
- 数据库课程设计-----------学生选课管理系统的设计
- 链接MYSql数据库的学生选课管理系统
- 关于学生管理系统的简单操作(数据库应用)
- Java 基于MySQL数据库的简单学生管理系统
- java-IO 文件作为数据库的学生管理系统
- 用C++写的“学生管理系统”
- 用xml代替数据库的一个小项目--学生管理系统
- 学生管理系统验收出现的问题及解决方法
- 学生管理系统中遇到的问题总结(一)
- 103_复习NetworkImageView
- usaco奶牛家谱
- Qt笔记之QGADGET
- java新手入门视频
- [Android]百度地图之几何标注和文字覆盖物
- 用数据库作学生管理系统时问题的解决
- Windows平台下Doxygen+GraphViz+HtmlHelp自动生成函数调用关系图
- 内存管理
- 使用requests模块模拟客户端请求
- 【Qt界面个性化】大杀器——qss
- 问题记录-python写mapper测试时出现urllib.error.HTTPError: HTTP Error 404: Not Found
- RHEL7文件权限管理
- 单链表逆向打印
- centos6.5系统bash损坏之救援模式修复