江苏省建设网站一号通,wordpress积分等级,淮南专业网站建设,写作平台有哪些#x1f34e;道阻且长#xff0c;行则将至。#x1f353; 目录
一、DriverManager 驱动管理类
1.注册驱动
2.获取数据库连接
二、Connection 数据库连接对象
1.获取执行对象
2.事务管理
三、Statement
1.执行DDL、DML语句
2.执行DQL语句
四、ResultSet 以JDBC快速…道阻且长行则将至。 目录
一、DriverManager 驱动管理类
1.注册驱动
2.获取数据库连接
二、Connection 数据库连接对象
1.获取执行对象
2.事务管理
三、Statement
1.执行DDL、DML语句
2.执行DQL语句
四、ResultSet 以JDBC快速入门的例子为引使用JDBC有驱动管理连接对象执行SQL、查询语句等下面将对JDBC中常见API进行解释。 public static void main(String[] args) throws Exception {//1. 注册驱动Class.forName(com.mysql.jdbc.Driver);//2. 获取连接String url jdbc:mysql://127.0.0.1:3306/db1;String username root;String passd 1234;Connection conn DriverManager.getConnection(url, username, passd);//3. 定义sqlString sql update account set money 2000 where id 1;//4. 获取执行sql的对象 StatementStatement stmt conn.createStatement();//5. 执行sqlint count stmt.executeUpdate(sql);//受影响的行数//6. 处理结果System.out.println(count);//7. 释放资源stmt.close();conn.close();} 一、DriverManager 驱动管理类
1.注册驱动
在入门案例中我们使用的是 Class.forName(com.mysql.jdbc.Driver); 先跳转到Driver具体来看在mysql.jdbc中的Driver类中
public class Driver extends NonRegisteringDriver implements java.sql.Driver {public Driver() throws SQLException {}static {try {DriverManager.registerDriver(new Driver());} catch (SQLException var1) {throw new RuntimeException(Cant register driver!);}}
}
静态代码块执行 DriverManager 对象的 registerDriver() 方法进行驱动的注册只要加载 Driver 类该静态代码块就会执行。案例中的Class.forName(com.mysql.jdbc.Driver); 就会加载 Driver 类。 p在MySQL 5之后的驱动包可以省略注册驱动的步骤 自动加载jar包中META-INF/services/java.sql.Driver文件中的驱动类。 2.获取数据库连接
使用的是DriverManager的getConnection方法方法有三个参数String url, String user, String password分别是数据库连接路径、用户名及密码。 CallerSensitivepublic static Connection getConnection(String url,String user, String password) throws SQLException {java.util.Properties info new java.util.Properties();if (user ! null) {info.put(user, user);}if (password ! null) {info.put(password, password);}return (getConnection(url, info, Reflection.getCallerClass()));}
连接路径url通常我们这样写 jdbc:mysql://ip地址(或域名):端口号/数据库名称?参数键值对1参数键值对2 对于本地mysql服务器且默认端口3306可以简写jdbc:mysql:///数据库名称?参数键值对 添加参数键值对useSSLfalse禁用安全连接方式可以解决警告提示。对于多个键值对使用连接。
二、Connection 数据库连接对象
作用是获取执行 SQL 的对象、管理事务。
1.获取执行对象
普通执行SQL对象—Statement
//4. 获取执行sql的对象 Statement
Statement stmt conn.createStatement();
还有不是普通的 预编译SQL的执行SQL对象 Creates a CallableStatement object for calling database stored procedures.The CallableStatement object provides methods for setting up its IN and OUT parameters, and methods for executing the call to a stored procedure. 创建用于调用数据库存储过程的CallableStatement对象。CallableStatement对象提供了设置其IN和OUT参数的方法以及执行对存储过程的调用的方法。 注意:此方法针对处理存储过程调用语句进行了优化。当prepareCall方法完成时某些驱动程序可能会将call语句发送到数据库其他的可以等到CallableStatement对象执行完。这对用户没有直接影响但是它确实会影响哪个方法抛出某些SQL Exceptions。 执行存储过程的 Converts the given SQL statement into the systems native SQL grammar. A driver may convert the JDBC SQL grammar into its systems native SQL grammar prior to sending it. This method returns the native form of the statement that the driver would have sent. 将给定的SQL语句转换成系统的本机SQL语法。驱动程序可以在发送之前将JDBC SQL语法转换成其系统的本地SQL语法。 p:存储过程在MySQL中不常用。 2.事务管理
MySQL有以下事务管理的操作 开启事务 BEGIN; START TRANSACTION; 提交事务 COMMIT; 回滚事务 ROLLBACK; JDBC在Connection接口中定义了3个对应的事务管理方法 void setAutoCommit(boolean autoCommit) throws SQLException;
//开启事务需要将该参数设为为false
void commit() throws SQLException;
void rollback() throws SQLException; 执行一个简单事务例子
//定义了两个要执行的sql
String sql update account set money 2000 where id 2;
String sql2 update account set money 2000 where id 1;// 开启事务 【连接conn、执行sql的对象 Statement stmt】
try {conn.setAutoCommit(false);int count stmt.executeUpdate(sql);//受影响的行数System.out.println(count);int count2 stmt.executeUpdate(sql2);//受影响的行数System.out.println(count2);
}catch (Exception throwables){conn.rollback();
}
三、Statement
作用就是执行SQL语句。
1.执行DDL、DML语句
例如上一个例子中的 int count stmt.executeUpdate(sql); executeUpdate就是用于执行DDL和DML的。
2.执行DQL语句
对于一个DQL查询语句使用是executeQuery String sql select * from account; ResultSet rsstmt.executeQuery(sql); 这个方法使用到了 ResultSet结果集 对象封装了SQL查询语句的结果。
四、ResultSet
上一节提到ResultSet封装了SQL查询语句的结果。我们打开ResultSet接口其中定义了next和一些getXxx方法来操作查询结果 boolean next() throws SQLException;String getString(int columnIndex) throws SQLException;
boolean getBoolean(int columnIndex) throws SQLException;
byte getByte(int columnIndex) throws SQLException;
int getInt(int columnIndex) throws SQLException;
double getDouble(int columnIndex) throws SQLException;
......
String getString(String columnLabel) throws SQLException;
...... next()将光标从当前位置向前移动一行判断当前行是否为有效行。就是找数据库表下一行 方法返回值 true 当前行有数据 false 当前行没有数据 getXxx(参数)获取数据。 参数有两种类型 int类型的参数列的编号从1开始 String类型的参数 列的名称 例定义查询sql并显示查询内容
String sql select * from account; close 最后也是需要释放资源。 void close() throws SQLException; rs.close(); ☕物有本末事有终始知所先后。