点击右边栏的database
这里有一点需要注意,就是连接不上,报了个错,错误显示 Connection to stu@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.
错误的原因是MySQL的驱动不一致,MySQL用的是 5.+ 的,而在idea中下载的是 8.+ 的,这里只需按照下面选择5.1即可
再次测试连接,ok
代码方式:
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Jdbc {
Connection connection = null;
Statement statement = null;
public void test(){
try{
String driver = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3306/stu?useSSL=false";
String USER = "用户名";
String PASSWORD = "密码";
Class.forName(driver);
connection = DriverManager.getConnection(URL,USER,PASSWORD);
System.out.println("连接成功");
statement = (Statement) connection.createStatement();
}catch (Exception e){
e.printStackTrace();
}
try {
String sSQL = "SELECT * FROM student";//查询数据库表信息
ResultSet resultSet = statement.executeQuery(sSQL);
while (resultSet.next()){
System.out.println(resultSet.getString(2));
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
Jdbc jdbc = new Jdbc();
jdbc.test();
}
}
运行这个代码的时候有时候会报错,报错信息为:
java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1785)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3496)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2385)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Jdbc.test(Jdbc.java:18)
at Jdbc.main(Jdbc.java:39)
null
从而拿到的结果是null值,主要错误是这个:
Initial client character set can be forced via the 'characterEncoding' property.
没有设置这个characterEncoding属性,只要在下面这句话的后面加个**&characterEncoding=utf8**就可以了
String URL = "jdbc:mysql://localhost:3306/stu?useSSL=false&characterEncoding=utf8";
测试: