日期:2014-05-20  浏览次数:20889 次

一个最简单的JDBC程序的问题
package zixue.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class Base {


static void test() throws SQLException,ClassNotFoundException{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());

//建立连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbc","root","woaiyan");

//创建语句
Statement st = conn.createStatement();

//执行语句
ResultSet rs = st.executeQuery("select * from user");

//处理结果
while(rs.next()){
System.out.print(rs.getObject(1)+"\t"+rs.getObject(2)+
"\t"+rs.getObject(3)+"\t");
}
//释放资源
rs.close();
st.close();
conn.close();
}
}


这是我的代码
驱动也有加进去
但是每次运行都显示不出查询的结果
控制台的信息是:
<ConnectionProperties>
 <PropertyCategory name="Connection/Authentication">
  <Property name="user" required="No" default="" sortOrder="-2147483647" since="all versions">
  The user to connect as
  </Property>
  <Property name="password" required="No" default="" sortOrder="-2147483646" since="all versions">
  The password to use when connecting
  </Property>
  <Property name="socketFactory" required="No" default="com.mysql.jdbc.StandardSocketFactory" sortOrder="4" since="3.0.3">
  The name of the class that the driver should use for creating socket connections to the server. This class must implement the interface 'com.mysql.jdbc.SocketFactory' and have public no-args constructor.
  </Property>
。。。。(还有很多)


我研究了半天了 换过SQL 换过驱动 也是一样的~
还有 我用MySQL的dos界面中查询可以查出我得 JDBC数据库中得JDBC表

------解决方案--------------------
你执行下面的代码看看控制台输出什么异常或者rs为空的信息么?
Java code

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class Base {


    static void test() throws SQLException,ClassNotFoundException{
//注册驱动
        Class.forName("com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());

//建立连接
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/jdbc","root","woaiyan");

//创建语句
        Statement st = conn.createStatement();

//执行语句
        ResultSet rs = st.executeQuery("select * from user");

//处理结果
        if(rs==null){
             System.out.println("查询结果为空");
        }
        while(rs.next()){
            System.out.print(rs.getObject(1)+"\t"+rs.getObject(2)+
                    "\t"+rs.getObject(3)+"\t");
        }
//释放资源
        rs.close();
        st.close();
        conn.close();
    }

    public static void main(String[] args){
           try{
              test();
           }catch(Exception e){
                e.printStackTrace();
           }
    }
}