一、JDBC介绍
二、JDBC 快速入门
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
2、Connection
//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
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
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();
}
}
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();
}
}