JDBC原理及常见错误分析
1.JDBC:Java DataBase Connectivity 可以为多种关系型数据库DBMS 提供统一的访问方式,用Java来操作数据库
2.JDBC API 主要功能:
三件事,具体是通过以下类/接口实现:
DriverManager : 管理jdbc驱动
Connection: 连接(通过DriverManager产生)
- Statement(PreparedStatement) :增删改查 (通过Connection产生 )
- CallableStatement : 调用数据库中的 存储过程/存储函数 (通过Connection产生 )
Result :返回的结果集 (上面的Statement等产生 )
Connection产生操作数据库的对象:
- Connection产生Statement对象:createStatement()
- Connection产生PreparedStatement对象:prepareStatement()
- Connection产生CallableStatement对象:prepareCall();
Statement操作数据库:
增删改:executeUpdate()
查询:executeQuery();
ResultSet:保存结果集 select * from xxx
next():光标下移,判断是否有下一条数据;true/false
previous(): true/false
getXxx(字段名|位置):获取具体的字段值
PreparedStatement操作数据库:
public interface PreparedStatement extends Statement
因此
增删改:executeUpdate()
查询:executeQuery();
--此外
赋值操作 setXxx();
PreparedStatement与Statement在使用时的区别:
1.Statement:
sql
executeUpdate(sql)
2.
PreparedStatement:
sql(可能存在占位符?)
在创建PreparedStatement 对象时,将sql预编译 prepareStatement(sql)
executeUpdate()
setXxx()替换占位符?
3.jdbc访问数据库的具体步骤:
- 导入驱动,加载具体的驱动类
- 与数据库建立连接
- 发送sql,执行
- 处理结果集 (查询)
4.
数据库驱动
驱动jar 具体驱动类 连接字符串
MySQL mysql-connector-java-x.jar com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/数据库实例名
使用jdbc操作数据库时,如果对数据库进行了更换,只需要替换:驱动、具体驱动类、连接字符串、用户名、密码
5.
测试JDBC基本功能代码
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCDemo { private static final String URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT%2B8"; private static final String USERNAME = "root"; private static final String PWD = "password"; public static void update() throws ClassNotFoundException, SQLException {// 增删改 // a. 导入驱动,加载具体的驱动类 Class.forName("com.mysql.cj.jdbc.Driver"); // b.与数据库建立连接 Connection connection = DriverManager.getConnection(URL, USERNAME, PWD); // c.发送sql,执行增删改查 Statement stmt = connection.createStatement(); //增加 String sql = "insert into student values(2,'李四',21)"; //修改 String sql = "update student set name='张三' where id=1"; //删除 String sql = "delete from student where id=1"; int count = stmt.executeUpdate(sql); if (count > 0) { System.out.println("操作成功!"); } stmt.close(); connection.close(); } public static void query() throws ClassNotFoundException, SQLException {// 增删改 // a. 导入驱动,加载具体的驱动类 Class.forName("com.mysql.cj.jdbc.Driver"); // b.与数据库建立连接 Connection connection = DriverManager.getConnection(URL, USERNAME, PWD); // c.发送sql,执行增删改[查] Statement stmt = connection.createStatement(); char stuname='a'; //模糊查询 String sql = "select id,name,age from student where name like '%"+stuname+"%'"; ResultSet rs=stmt.executeQuery(sql); //int count = stmt.executeUpdate(sql); while (rs.next()) { int id=rs.getInt("id"); String name=rs.getString("name"); int age=rs.getInt("age"); System.out.println(id+"--"+name+"--"+age); } rs.close(); stmt.close(); connection.close(); } public static void main(String[] args) throws ClassNotFoundException, SQLException { // update(); query(); } }
错误记录:
Exception in thread "main" java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228) at JDBCDemo.query(JDBCDemo.java:35) at JDBCDemo.main(JDBCDemo.java:57)
解决办法
在连接数据库的URL结尾添加 ?serverTimezone=GMT%2B8 即可