第1关:JDBC更新员工密码
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c09b9884b86132b60346fa71e705f48.png)
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查询员工信息
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cd25b3e10170c5aa864e2f169b72d244.png)
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();
}
}
}
}