/**
*
* @param conn
* @param pst
* @param sql
*/
public void inputInfo(Connection conn, PreparedStatement pst,String sql){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String strs[] = str.split(" ");
try {
pst.setString(1, strs[1]);
pst.setString(2, strs[2]);
pst.setString(3, strs[3]);
pst.setString(4, strs[4]);
pst.setInt(5,Integer.parseInt(strs[0]));
pst.execute();
list();
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionSql.getClose(conn, pst, rs);
}
}
/**
* 删除学生信息
*/
// @Test
public void delete(){
list();
System.out.println("请输入要删除的编号:");
Scanner scanner = new Scanner(System.in);
int sNo = scanner.nextInt();
conn = ConnectionSql.getConnection();
String sql = "delete from student where sNo = ?";
try {
pst = conn.prepareStatement(sql);
pst.setInt(1, sNo);
pst.execute();
list();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionSql.getClose(conn, pst, rs);
}
}
/**
* 根据 结果集遍历学生信息
* @param rs
*/
public void getStudentResultSet(ResultSet rs){
try {
System.out.println("当前学生列表:");
while( rs.next()){
int sNo = rs.getInt(1);
String sName = rs.getString(2);
String sSex = rs.getString(3);
String sClass = rs.getString(4);
String sBirthday = rs.getString(5);
System.out.println(sNo + " " + sName + " " + sSex + " " + sClass + " " + sBirthday );
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//连接数据库
public static Connection getConnection(){
Connection conn= null;
try {
Class.forName(DRIVER);
// System.out.println("链接数据库");
conn = (Connection) DriverManager.getConnection(URL, USER, PASS);
// System.out.println("链接数据库成功");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库
public static void getClose(Connection conn, PreparedStatement pst, ResultSet rs){
try {
if ( conn != null){
conn.close();
}
if ( pst != null){
pst.close();
}
if ( rs != null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}