JDBC
连接数据库
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");//可以不写
//2、获取连接对象
String url="jdbc:mysql://127.0.0.1:3306/db1?useSSL=false";
String username="root";
String password="xxxx";
Connection conn=DriverManager.getConnection(url,username,password);
//3、定义sql
String sql="update account set money = 20000 where id=1";
//4、获取sql对象
Statement stmt=conn.createStatement();
//5、执行sql
int count=stmt.executeUpdate(sql);
//6、处理结果
//System.out.println(count);
//7、释放资源
stmt.close();
conn.close();
api介绍
DriverManager(驱动管理类)
作用
1、注册驱动
Class.forName("com.mysql.jdbc.Driver");//可以不写
Driver里面会调用DriverManager的注册驱动函数
2、获取数据库连接
Connection conn=DriverManager.getConnection(url,username,password);
参数
1、url
1、语法
jdbc:mysql://ip地址:端口号/数据库名称?参数键值对1&参数键值对2.......
2、示例
jdbc:mysql://127.0.0.1:3306?db1
3、细节
如果连接的是本机且默认端口是3306可以简写成jdbc:mysql:///数据库名称?参数键值对1&参数键值对2.......
2、user
3、password
Connection
作用
1、获取执行SQL对象
普通执行sql对象
Statement createStament(sql)
预编译sql的执行的执行sql对象:防止sql注入
PreparedStatement prepareStatement(sql)
执行存储过程对象
CallableStatement prepareCall(sql)
2、管理事务
开始十五:setAutoCommit(boolean autoCommit):true为自动提交事务;false为手动提交事务即为开启事务
提交事务:commit()
回滚事务:rollback()
public class Demo4 {
public static void main(String args[]) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/db1?useSSL=false";
String username="root";
String password="xxxx";
Connection conn=DriverManager.getConnection(url,username,password);
String sql1="update account set money = 20000 where id=1";
String sql2="update account set money = 20000 where id=2";
Statement stmt=conn.createStatement();
try {
//开启事务
conn.setAutoCommit(false);
int count1=stmt.executeUpdate(sql1);
System.out.println(count1);
int i=3/0;//手动造异常
int count2=stmt.executeUpdate(sql2);
System.out.println(count2);
//提交事务
conn.commit();
} catch (Exception e) {
//回滚事务
conn.rollback();
e.printStackTrace();
}
stmt.close();
conn.close();
}
}
Statement
作用
1、执行sql语句
1、int executeUpdate(sql):执行DML(包括insert、delete、update)、DDL(数据库定义语句)语句
返回值:(1)DML返回语句影响行数(2)DDL语句执行后,执行成功可能返回0
2、ResultSet executeQuery(sql) :执行DQL(数据库检索)语句
返回值:ResultSet结果集对象
ResultSet
作用
获取查询结果
1、boolean next():(1)将光标从当前位置向前移动一行(判断当前行是否为有效行)
返回值:true有效行,当前行有数据;false 无效行,当前行没有数据
2、XXX getXXX(参数):获取参数
XXX:数据类型,如int getInt(参数),String getString (参数)
参数:
·int:列的编号,从1开始!!!
·String:列的名称
public class Demo4 {
public static void main(String args[]) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/db1?useSSL=false";
String username="root";
String password="xxxx";
Connection conn=DriverManager.getConnection(url,username,password);
//定义sql语句
String sql = "select * from account";
//创建Statement对象
Statement stmt = conn.createStatement();
//执行sql
ResultSet rs= stmt.executeQuery(sql);
//处理结果,遍历rs的所有数据
while(rs.next()){
int id=rs.getInt(1);//int id=rs.getInt("id");
String name= rs.getString(2);//String name= rs.getString("name");
double money = rs.getDouble(3);//double money = rs.getDouble("money");
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("==================");
}
rs.close();
stmt.close();
conn.close();
}
}
PreparedStatement
作用
1、预编译sql语句并执行,预防sql注入问题
sql注入
//登录逻辑
String name="";
Stirng pwd="";
String url="select * from db_user where username= '"+name+"' and password='"+pwd+"'";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()) System.out.println("登录成功");
else System.out.println("登录失败");
===================================================================================
//演示sql注入
String name="sadfgdg";
Stirng pwd="' or '1'='1";
此时sql=select * from db_user where username= 'sadfgdg' and password='' or '1'='1'
这是一个恒等式,会使表中全部输出
用法
1、获取PreparedStatement对象
//sql语句中的参数值,使用?占位符替代
String sql=“selext * from user where username = ? and password=?”;
//通过connection 对象获取,并传入对应的sql语句
PreparedStatement pstmt = conn.prepareStatement(sql);
2、设置参数值
PreparedStatement对象:setXxx(参数1,参数2):给?赋值
Xxx:数据类型;如setInt(参数1、参数2)
参数1:?的位置编号,从1开始!!!
参数2:?的值
3、执行sql
executeUpdate()/executeQuery(),不需要传递sql
4、使用时开启预编译功能
url=“jdbc:mysql://127.0.0.1:3306/db1?useSSl=false&useServerPrepstmts”;
数据库连接池——Druid (德鲁伊)
使用步骤
1、导入jar包
2、定义配置文件
3、加载配置文件
4、获取数据库连接池对象
5、获取连接
配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true
username=root
password=xxx
# 初始化连接常量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
代码
public static void main(String[] args) throws Exception {
// 1、导入jar包
// 2、定义配置文件
// 3、加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-app/src/druid.properties"));
// 4、获取数据库连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 5、获取连接
Connection conn= dataSource.getConnection();
System.out.println(conn);
}