```package com.web.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;;
public class demo2 {
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Connection conn=DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3309/", "root", "123"); System.out.println(conn!=null?"取得":"未取得"); //创建封装sql命令的对象 String sql ="select * from ml"; Statement stmt=conn.createStatement(); //执行sql命令,并返回符合条件的记录集合 ResultSet rs=stmt.executeQuery(sql); //处理结果集合 while(rs.next()) { Object obj=rs.getObject("name"); System.out.println(obj); } //依次关闭所用过的连接对象 rs.close(); stmt.close(); conn.close(); }
}
运行的话会显示这句话出错 ResultSet rs=stmt.executeQuery(sql);
运行结果:
取得 Exception in thread "main" java.sql.SQLException: No database selected at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370) at com.web.jdbc.demo2.main(demo2.java:21)
实在是不知道咋改了,求大神指点
大话西游666
婷婷同学_
ibeautiful
紫衣仙女
慕森王
相关分类