Jdbc整理
jdbc连接数据库方式一
public class JdbcDemo {
public static void main(String[] args) throws Exception {
//1.导入jar包
//2.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection
("jdbc:mysql:///test?serverTimezone=GMT%2B8", "root", "123456");
//定义sql语句
String sql = "update users.user set password='1234' where uid=3";
//获取执行sql的对象
Statement stmt = conn.createStatement();
//执行sql
int conut = stmt.executeUpdate(sql);
//处理结果
System.out.println(conut);
//释放资源
stmt.close();
}
}
碰到的问题:
1.驱动配置错误,原配置com.mysql.jdbc.Driver,正确的应该是com.mysql.cj.jdbc.Driver
2.时区的问题,原配置jdbc:mysql:///test", “root”, “123456,正确的应该是jdbc:mysql:///test?serverTimezone=GMT%2B8”, “root”, "123456
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.
jar包可以直接在网上找,这里配置的jar包是最新的jar包