JDBC访问数据库步骤:
•1:加载一个Driver驱动
•2:创建数据库连接(Connection)
•3 : 创建SQL命令发送器Statement
•4:通过Statement发送SQL命令并得到结果
•5:处理结果(select语句)
•6:关闭数据库资源
•ResultSet
•Statement
•Connection
例子:
public class TestSelect {
public static void main(String[] args) {
//0.将相应数据库的jar包放入项目
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
int n=0;
try{
//1.加载驱动(MySQL)
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/stumgr";
String user = "root";
String password = "root";
Class.forName(driver);
//2.建立(和数据库)连接
conn = DriverManager.getConnection(url,user,password);
//3.创建一个SQL命令发送器
stmt = conn.createStatement();
//4.使用SQL命令发送器发送SQL命令(子弹)并得到结果
String sql = "select * from dept where deptno > 20";
rs = stmt.executeQuery(sql);
//5.处理结果
System.out.println("编号\t名称\t地址");
while(rs.next()){
//获取各列的数据
int deptno = rs.getInt(1);
String dname = rs.getString(2);
String location = rs.getString(3);
//输出各列的数据
System.out.println(deptno+"\t"+dname+"\t"+location);
}
}catch(SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
//6.关闭各种数据库资源
try {
if(stmt != null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
解决SQL注入风险
PrepareStatement:
安全性高,可以避免SQL注入
简单不繁琐,不用进行字符串拼接
性能高,用在执行多个相同数据库DML操作时
将创建SQL命令发送器Statement改为PrepareStatement
public static User login(String userId,String pwd){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
User user = null;//默认登录失败
try{
//1.加载驱动(MySQL)
//2.建立(和数据库)连接
//3.创建一个SQL命令发送器
String sql ="select * from t_user where userid = ? and password = ?"; //? 占位符
pstmt = conn.prepareStatement(sql);
//4.使用SQL命令发送器来发送SQL命令(子弹)并得到结果
pstmt.setString(1,userId);
pstmt.setString(2,pwd);
rs = pstmt.executeQuery();
//5.处理结果
if(rs.next()){ //登录成功,查询到了数据
//获取当前行各个列的数据
String realName = rs.getString("realname");
double money = rs.getDouble("money");
//将当前行各个列的数据封装到一个User对象中
user = new User(userId,realName,null,money);
}
}catch(SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
//6.关闭各种数据库资源
}
return user;
}
使用事务保证转账安全性
- conn.setAutoCommit(false); 关闭事务自动提交
- conn.commit(); 事务提交
- conn.rollback(); 事务回滚
public class TestTransaction {
public static void main(String[] args) {
//0.将相应数据库的jar包放入项目
Connection conn = null;
Statement stmt = null;
int n=0;
try{
//1.加载驱动(MySQL)
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/stumgr";
String user = "root";
String password = "root";
Class.forName(driver);
//2.建立(和数据库)连接
conn = DriverManager.getConnection(url,user,password);
//3.创建一个SQL命令发送器
stmt = conn.createStatement();
//4.使用SQL命令发送器来发送SQL命令并得到结果
//事务不再自动结束,需要手动的提交或者回滚
conn.setAutoCommit(false);
stmt.executeUpdate("update t_user set money = money -2000 where userid = 'zhangsan'");
stmt.executeUpdate("update t_user set money = money1 +2000 where userid = 'lisi'");
//手动提交事务,能执行该语句,表明前面多个DML操作都可以成功操作,只是数据只是写入缓存,还没有真正写入数据库
conn.commit();
}catch(SQLException e){
//手动的回滚事务,回到所有DML操作执行之前的状态
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
//6.关闭各种数据库资源
//省略
}
}
}
}