1.请教一下老师度数据库的问题
来源:4-6 ——完成一个业务流程:注册

Ironxi_work
2018-07-06
问题:测试用例从数据库读取数据不成功,数据库账号root,密码root123; 1.需求:从ocdb数据库中成功读取user表的数据,并在控制台展示出来; 2.设计 2.1 创建数据库: create table users ( id int unsigned auto_increment not null, first_name varchar(32) not null, last_name varchar(32) not null, date_created timestamp default now(), is_admin boolean, num_points int, primary key (id) ); 2.2 写Java测试程序: package com.imooc.MySQL.dao; import java.sql.*; public class GetMySQLTable { public static void main(String[] args) { try { // create our mysql database connection String myDriver = "com.mysql.jdbc.Driver"; String myUrl = "dbc:mysql://localhost:3306/ocdb?useUnicode=true&characterEncoding=utf-8"; Class.forName(myDriver); Connection conn = DriverManager.getConnection(myUrl, "root", "root123"); // our SQL SELECT query. // if you only need a few columns, specify them by name instead of // using "*" String query = "SELECT * FROM users"; // create the java statement Statement st = conn.createStatement(); // execute the query, and get a java resultset ResultSet rs = st.executeQuery(query); // iterate through the java resultset while (rs.next()) { int id = rs.getInt("id"); String firstName = rs.getString("first_name"); String lastName = rs.getString("last_name"); Date dateCreated = rs.getDate("date_created"); boolean isAdmin = rs.getBoolean("is_admin"); int numPoints = rs.getInt("num_points"); // print the results System.out.format("%s, %s, %s, %s, %s, %s\n", id, firstName, lastName, dateCreated, isAdmin, numPoints); } st.close(); } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); } } } 3.结果: console显示: Got an exception! com.mysql.jdbc.Driver
写回答
1回答
-
祁聪
2018-07-07
首先检查用户名/密码是不是正确;
再检查当前用户的是不是有读写权限;
最好看一下具体是什么异常;
012018-07-11
相似问题