本人java菜鸟一名 ,通过写博客记录一下自己的学习过程,本次和大家分享一下自己在连接数据库时的出现的问题。
安装社区服务器版:
官网下载地址:https://dev.mysql.com/downloads/
参考网上的安装教程:https://www.cnblogs.com/ayyl/p/5978418.html
安装完成之后会发现,整个界面是这样的,可以通过Open Inspector来查看基本的信息,插入数据等非常的不方便。
这和我们当初学数据库时,操作的界面完全不一样,根本不晓得点哪里,楼主无可奈何之下,新建了query查询,用一下语句进行了数据库的新建:
create database libmanage;
create table student(
Sno int primary key,
Sname char(9) not null,
Ssex char(2)
);
然后通过插入和查询来满足了我对数据库的需求,然而楼主通过这种笨重的方式玩了很久,突然一天好友来拜访时,才解决了这个问题。我下载的是Mysql的社区服务器版,所以操作很不方便,那么我们需要下载一下客户端版,就会使我的操作更加简单;
大家可以自行搜索一下Mysql客户端,推荐大家使用navicat,他支持MySql,Oracel,SQL server等主流数据库,很方便。
下载和激活教程:https://www.cnblogs.com/joyny/p/9536006.html,这个教程他提供了交互界面,不需要在CMD窗口中操作;实测成功;这个软件可以更加方便帮我管理数据库,使用界面如下:
此处需要下载一个驱动包,即mysql-connector-java-8.0.15.jar,并将其导入到工程下面来;
链接:https://pan.baidu.com/s/1zY_EgW00KXNrmibgBDnR2w
提取码:hhjm
网上的很多教程讲到了如何连接数据库,如下:
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/libmanage";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123456";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from emp";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
String job = null;
String id = null;
while(rs.next()){
//获取stuname这列数据
job = rs.getString("job");
//获取stuid这列数据
id = rs.getString("ename");
//输出结果
System.out.println(id + "\t" + job);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
此段代码在运行时,提示这样的错误:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time
需要我们将com.mysql.jdbc.Driver替换成新的com.mysql.cj.jdbc.Driver;多了一个cj,即这样定义:
String driver = "com.mysql.cj.jdbc.Driver";
更改之后发现又有新的错误,错误为:
java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time
他的意思是时间戳不匹配,我们只需要在url后面加上和时间相关的配置信息:
String url = "jdbc:mysql://127.0.0.1:3306/libmanage?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai";
运行结果如下:
整个代码如下:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnect{
public static void main(String[] args) {
Connection con;
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/libmanage?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai";
String user = "root";
String password = "123456";
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed()) {
System.out.println("succeeded connecting to the Database!");
}
Statement statement = con.createStatement();
String sql = "select * from reader";
ResultSet rs = statement.executeQuery(sql);
String Sno = null;
String Sname = null;
while(rs.next()) {
Sno = rs.getString("Rno");
Sname = rs.getString("Rname");
System.out.println(Sno + " " + Sname);
}
rs.close();
con.close();
}
catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
}catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}
不足之处,希望大家积极指出,自己在学习过程中发现的一些问题,也会及时和大家交流,帮助后面学习的你。