一、JDBC介绍
![](https://i-blog.csdnimg.cn/blog_migrate/a322fb073f4edfb7cb51a131b87bd8f9.png)
二、JDBC 快速入门
![](https://i-blog.csdnimg.cn/blog_migrate/da3b0d69dad403b9225ae02529292792.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6323f0aec62391eac89a21db467e3da1.png)
package com.itheima.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDCBDemo {
public static void main(String[] args) throws Exception {
//1、注册驱动
Class.forName("com.msql.jdbc.Driver");
//2、获取链接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//3、定义sql
String sql = "update account set money = 2000 where id = 1";
//4、获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
//5、执行sql
int count = stmt.executeUpdate(sql);//受影响的行数
//6、处理结果
System.out.println(count);
//7、释放资源
stmt.close();
conn.close();
}
}
三、JDBC API详解
1、DriverManager
![](https://i-blog.csdnimg.cn/blog_migrate/aa4adf92d3abfce0ce6983ec040089dd.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4383520b72e8f6d69c36aab985cc12bc.png)
2、Connection
![](https://i-blog.csdnimg.cn/blog_migrate/2ce067b96ed1f944b4725b2ede8be022.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f61dc68bd2cdeedde493b3f3eeb9d64d.png)
//3、定义sql
String sql1 = "update account set money = 3000 where id = 1";
String sql2 = "update account set money = 3000 where id = 2";
Statement stmt = conn.createStatement();
try {
//开启事物
conn.setAutoCommit(false);
int count1 = stmt.executeUpdate(sql1);
System.out.println(count1);
int count2 = stmt.executeUpdate(sql2);
System.out.println(count2);
//提交事物
conn.commit();
} catch (Exception throwables) {
//回滚事物
conn.rollback();
throwables.printStackTrace();
}
//7、释放资源
stmt.close();
conn.close();
3、Statement
![](https://i-blog.csdnimg.cn/blog_migrate/c0591de5f846fa27aa30d67d2176a319.png)
![](https://i-blog.csdnimg.cn/blog_migrate/93f505c80148e7afc00885bf00b6bc16.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8b68d6f3e0dfbc4e6b2ca16ce95d24a4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6ed8d4484beffc2065078c3ed0ebb96a.png)
package com.itheima.jdbc;
import java.sql.*;
public class JDCBDemo {
public static void main(String[] args) throws Exception {
//1、注册驱动
Class.forName("com.msql.jdbc.Driver");
//2、获取链接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//3、定义sql
String sql = "select * from account";
//4、获取statememt对象
Statement stmt = conn.createStatement();
//5、执行sql
ResultSet rs = stmt.executeQuery(sql);
//6、处理结果,遍历rs中的所有数据
//6.1 光标向下移动一行,并且判断当前是否有数据
while (rs.next()){
//6.2 获取数据 getXxx
int id = rs.getInt(1);
String name = rs.getString(2);
double money = rs.getDouble(3);
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("----------------------");
}
//7、释放资源
rs.close();
stmt.close();
conn.close();
}
}
4、PrepareStatement
![](https://i-blog.csdnimg.cn/blog_migrate/642aa7bc16ceadcd04d37899046b5e4d.png)
package com.itheima.jdbc;
import java.sql.*;
public class JDCBDemo {
public static void main(String[] args) throws Exception {
//1、注册驱动
Class.forName("com.msql.jdbc.Driver");
//2、获取链接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//接受用户输入 用户名和密码
String name = "zhangsan";
String pwd = "123";
String sql = "select * from tb_user where username = '" + name + "' and password = '" + pwd + "'";
//获取stmt对象
Statement stmt = conn.createStatement();
//执行sql
ResultSet rs = stmt.executeQuery(sql);
//判断是否登录成功
if (rs.next()){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
//释放资源
rs.close();
stmt.close();
conn.close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/59c126747a5ff949e5e97cbc40b8ea91.png)
package com.itheima.jdbc;
import java.sql.*;
public class JDCBDemo {
public static void main(String[] args) throws Exception {
//1、注册驱动
Class.forName("com.msql.jdbc.Driver");
//2、获取链接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//接受用户输入 用户名和密码
String name = "zhangsan";
String pwd = "123";
//定义sql
String sql = "select * from tb_user where username = ? and password = ?";
//获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置 ? 的值
pstmt.setString(1,name);
pstmt.setString(2,pwd);
//执行sql
ResultSet rs = pstmt.executeQuery();
//判断是否登录成功
if (rs.next()){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
//释放资源
rs.close();
pstmt.close();
conn.close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/dd1686b5b25330eeae098ee7db4f8484.png)