做一个班级管理系统的java程序,怎么写代码
- 提问者网友:骑士
- 2021-08-21 17:06
- 五星知识达人网友:北城痞子
- 2021-08-21 18:43
package pack;
import java.sql.*;
import java.util.*;
public class DataBaseOperation {
private static Connection conn;
public void ins(Student[] students){ for(int i=0; i<students.length; i++) ins(students[i]);
} void ins(Student student){ String sql = "insert into student values("; sql += "'" + student.getName() + "',"; sql += student.getAge() + ",'"; sql += student.getNum() + "','"; sql += student.getSex() + "')";
connect(); try{ Statement stmt = conn.createStatement(); stmt.executeUpdate(sql); }catch(SQLException e){} close();
}
void del(String name){ String sql = "delete from student where Name=" ; sql+="'"+name+"'";
connect(); try{ Statement stmt = conn.createStatement(); stmt.executeUpdate(sql); }catch(SQLException e){} close();
}
public Student[] getStudent(){ ArrayList list = new ArrayList(); String sql = "select * from student"; connect(); try{ Statement stmt = conn.createStatement(); ResultSet rst = stmt.executeQuery(sql); while(rst.next()){ String name = rst.getString(1); int age = rst.getInt(2); int number = rst.getInt(3); String sex= rst.getString(4); Student p = new Student(name); p.setAge(age);p.SetNum(number); p.setSex(sex); list.add(p); } rst.close(); }catch(SQLException e){} return (Student[])list.toArray(new Student[0]);
}
public static void connect(){ String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:xiao"; try{ Class.forName(driverName); conn = DriverManager.getConnection(url); }catch(ClassNotFoundException e){e.getMessage(); }catch(SQLException e){ e.getMessage(); }
}
private void close(){ try{ if(conn!=null) conn.close(); }catch(SQLException e){e.getMessage();}
}
}
还要不?