Topic: 数据库连接成功,但是某行出现异常

  Print this page

1.数据库连接成功,但是某行出现异常 Copy to clipboard
Posted by: RingofAngel
Posted on: 2005-07-20 18:24

小弟愚钝,实在看不出来33行有什么问题。而且对于异常不是很了解,所以请教。
之所以用Object[][] cells,是为了原本是向生成一个表的,所以临时写了这个程序
看看能不能读到数据。

import java.sql.*;
import java.awt.*;
public class DBConnection{
public static void main(String args[]){
Connection con=null;
  Statement stmt=null;
  String no="",name="";
ResultSet rs=null;
Object[][] cells=null;
  try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("驱动程序已装载");
    System.out.println("即将连接数据库");
  }catch(Exception ex){
    System.out.println("Can not load Jdbc Driver:"+ex.getMessage());
    return;
  }
  try{
    con=DriverManager.getConnection
("jdbc:oracle:thin:@172.17.10.4:1521:prod9i","doc","doc");
    DatabaseMetaData dmd=con.getMetaData();
stmt=con.createStatement();    
rs=stmt.executeQuery("select * from DOCUMENT_AFFAIR");
System.out.println("已连接到数据库:"+dmd.getURL());
   System.out.println("所用的驱动程序是:"+dmd.getDriverName());
int i=0;
while(!rs.next()){
for(int j=0;j<7;j++)
cells[i][j]=rs.getObject(j+1);
i++;
}
for(i=0;i<2;i++){
for(int j=0;j<7;j++)
System.out.println("It is"+cells[i][j]);
}
}catch(SQLException ex){
    System.out.println("failed to Connect");
    System.out.println(ex.getMessage());
  }
}
}

此段程序编译通过了,能生成class
运行之后显示

驱动程序已装载
即将连接数据库
已连接到数据库:jdbc:oracle:thin:@172.17.10.4:1521:prod9i
所用的驱动程序是:Oracle JDBC driver
Exception in thread "main" java.lang.NullPointerException
at DBConnection.main(DBConnection.java:33)

2.Re:数据库连接成功,但是某行出现异常 [Re: RingofAngel] Copy to clipboard
Posted by: jimmy14
Posted on: 2005-07-20 23:11

your cells is null,
try modify line 9

Object[][] cells=new Object[2][7];

3.Re:数据库连接成功,但是某行出现异常 [Re: jimmy14] Copy to clipboard
Posted by: RingofAngel
Posted on: 2005-07-21 16:41

OK,success!!!

But ,if I do not know how many rows in the table,how to amend?

4.Re:数据库连接成功,但是某行出现异常 [Re: RingofAngel] Copy to clipboard
Posted by: sendtome
Posted on: 2005-07-21 22:17

初始化数组的时间推迟到你获得resultset后就知道有多少rows了。

5.Re:数据库连接成功,但是某行出现异常 [Re: RingofAngel] Copy to clipboard
Posted by: xiaoqiang806
Posted on: 2005-08-07 19:24

先运用 rs.last();
int totalNum=rs.getRow();


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923