第1关:JDBC更新员工密码
package step1 ;
import java. sql. * ;
public class UpdatePass {
public static void updateDB ( ) {
try {
Class . forName ( "com.mysql.jdbc.Driver" ) ;
} catch ( ClassNotFoundException e1) {
e1. printStackTrace ( ) ;
}
Connection conn = null ;
PreparedStatement ps = null ;
try {
String url = "jdbc:mysql://127.0.0.1:3306/tsgc?useUnicode=true&characterEncoding=utf8" ;
String user = "root" ;
String password = "123123" ;
conn = DriverManager . getConnection ( url, user, password) ;
String sql = "update employee set password='hello' where sex='女'" ;
ps = conn. prepareStatement ( sql) ;
ps. execute ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
ps. close ( ) ;
conn. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
}
第2关:JDBC查询员工信息
package step1 ;
import java. sql. * ;
public class QueryPass {
public static void queryDB ( ) {
Connection conn = null ;
PreparedStatement ps = null ;
try {
Class . forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://127.0.0.1:3306/tsgc?useUnicode=true&characterEncoding=utf8" ;
String user = "root" ;
String password = "123123" ;
conn = DriverManager . getConnection ( url, user, password) ;
String sql = "select * from employee" ;
ps = conn. prepareStatement ( sql) ;
ResultSet rs = ps. executeQuery ( ) ;
while ( rs. next ( ) ) {
String no = rs. getString ( 1 ) ;
String name = rs. getString ( 2 ) ;
Object password1 = rs. getString ( 3 ) ;
Object sex = rs. getString ( 4 ) ;
double salary = rs. getDouble ( 5 ) ;
System . out. println ( "no:" + no + "\tname:" + name + "\tpassword:" + password1 + "\tsex:" + sex
+ "\tsalary:" + salary) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
ps. close ( ) ;
conn. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
}