1.增加信息
public static int regStu ( Student student) {
int result = - 1 ;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
Connection conn = DriverManager. getConnection ( url, user, password) ;
String sql = "insert into student(name,pass,grade,age) values(?,?,?,?)" ;
PreparedStatement pstmt = conn. prepareStatement ( sql) ;
pstmt. setString ( 1 , student. getName ( ) ) ;
pstmt. setString ( 2 , student. getPass ( ) ) ;
pstmt. setString ( 3 , student. getGrade ( ) ) ;
pstmt. setInt ( 4 , student. getAge ( ) ) ;
result = pstmt. executeUpdate ( ) ;
if ( result == 1 ) {
System. out. println ( "注册成功" ) ;
} else {
System. out. println ( "注册失败" ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return result;
}
2.删除信息
public static int delStu ( int stuId) {
int result = - 1 ;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
Connection conn = DriverManager. getConnection ( url, user, password) ;
String sql = "delete from student where id = ?" ;
PreparedStatement pstmt = conn. prepareStatement ( sql) ;
pstmt. setInt ( 1 , stuId) ;
result = pstmt. executeUpdate ( ) ;
if ( result == 1 ) {
System. out. println ( "删除成功" ) ;
} else {
System. out. println ( "删除失败" ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return result;
}
3.更新信息
public static int updatStu ( String stuPass, int stuId) {
int result = - 1 ;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
Connection conn = DriverManager. getConnection ( url, user, password) ;
String sql = "update student set pass = ? where id = ?" ;
PreparedStatement pstmt = conn. prepareStatement ( sql) ;
pstmt. setString ( 1 , stuPass) ;
pstmt. setInt ( 2 , stuId) ;
result = pstmt. executeUpdate ( ) ;
if ( result == 1 ) {
System. out. println ( "更新成功" ) ;
} else {
System. out. println ( "更新失败" ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return result;
}