这个错误是怎么回事啊 代码是跟着老师打的

qq_i02xjug8 MySQL 入门 最后由 极客学院_吕布 于08月28日回复

  • 1 回答
  • 137 浏览

package com.jikexueyuan.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class JDBCTest {

public static void main(String[] args) {

      String sql = "SELECT * FROM tbl_user";

      Connection conn = null;

      Statement st= null;

      ResultSet rs= null;

      

      try {

      Class.forName ("com.mysql.cj.jdbc.Driver");

      conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=Hongkong","root","");

      st = conn.createStatement();

      rs = st.executeQuery(sql);

      while(rs.next()) {

      System.out.print(rs.getInt("id")+" ");

      System.out.print(rs.getString("name")+" ");

      System.out.print(rs.getString("password")+" ");

      System.out.print(rs.getString("email")+" ");

      System.out.println();

      }

      }catch(Exception e){

      e.printStackTrace();

      }finally {

      try {

      rs.close();

      }catch (Exception e2) {

}

      try {

      st.close();

      }catch(Exception e3) {

      

      }

      try {

      conn.close();

      }catch(Exception e4) {

      }

      }

}

}

java.sql.SQLException: No database selected

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)

at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1247)

at com.jikexueyuan.jdbc.JDBCTest.main(JDBCTest.java:21)