先导包
什么数据库倒什么样的包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
创建连接执行sql
加载驱动
连接数据库
创建发送sql的Statement
编写sql
执行sql,返回结果集
关闭连接,先创建的连接后关闭
连接方式:statement执行
import java.sql.*;
public class JDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/jdbc>useUnicode=true";
String username="root";
String password = "root";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection connection = DriverManager.getConnection(url, username, password);
//向数据库发送SQL的Statement
Statement statement = connection.createStatement();
//编写sql
String sql = "select * from user";
//执行sql,返回结果集
ResultSet resultSet = statement.executeQuery(sql);
String other = "update user set name='gyl'where id=1";
int otherresult = statement.executeUpdate(other);
while (resultSet.next()) {
String id = resultSet.getObject("id").toString();
String name = resultSet.getObject("name").toString();
String age = resultSet.getObject("age").toString();
String sex = resultSet.getObject("sex").toString();
}
//关闭连接 先创建的最后关闭
resultSet.close();
statement.close();
connection.close();
}
}
查询用statement.executeQuery(sql)
增删改用statement.executeUpdate(other);
JDBC事务
mysql数据库中执行
start TRANSACTION
UPDATE user set name="1" where id="0"
update yisheng set name="2" where id="0"
ROLLBACK
COMMIT;
在代码中实现(预编译方式)
String url = "jdbc:mysql://localhost:3306/jdbc>useUnicode=true";
String username="root";
String password = "root";
Connection connection=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
connection = DriverManager.getConnection(url, username, password);
//通知数据库开启事务 false为开启
connection.setAutoCommit(false);
String sql = "update yisheng set name=\"2\" where id=\"0\"";
connection.prepareStatement(sql).executeUpdate();
//制造一个错误
int i = 1 / 0;
String sql2 = "update user set name=\"2\" where id=\"0\"";
connection.prepareStatement(sql).executeUpdate();
//如果两条sql都执行成功,则提交事务。
connection.commit();
}catch (Exception e){
//如果有异常则回滚
connection.rollback();
}finally {
//关闭连接 先创建的最后关闭
connection.close();
}
两种连接连接方式的区别
statement
//向数据库发送SQL的Statement
Statement statement = connection.createStatement();
//编写sql
String sql = "select * from user";
//执行sql,返回结果集
ResultSet resultSet = statement.executeQuery(sql);
prepareStatement
String sql = "update yisheng set name=\"2\" where id=\"0\"";
connection.prepareStatement(sql).executeUpdate();
Statement接口提供了执行语句和获取结果的基本方法;
PreparedStatement接口添加了处理 IN 参数的方法;
CallableStatement接口添加了处理 OUT 参数的方法。Statement: 普通的不带参的查询SQL;支持批量更新,批量删除;
PreparedStatement: 可变参数的SQL,编译一次,执行多次,效率高; 安全性好,有效防止Sql注入等问题; 支持批量更新,批量删除;
继承自PreparedStatement,支持带参数的SQL操作;支持调用存储过程,提供了对输出和输入/输出参数(INOUT)的支持;
Statement每次执行sql语句,数据库都要执行sql语句的编译;
PreparedStatement是预编译的,使用PreparedStatement有几个好处
1:在执行可变参数的一条SQL时,PreparedStatement比Statement的效率高,因为DBMS预编译一条SQL当然会比多次编译一条SQL的效率要高。
2:安全性好,有效防止Sql注入等问题。
3:对于多次重复执行的语句,使用PreparedStament效率会更高一点,并且在这种情况下也比较适合使用batch;:4:代码的可读性和可维护性。