出错:JDBC报NullPointerException和Oracle报ORA-00936错误

我的程序如下,
/*
*数据库访问的封装类。
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.youcompany.struts.action;
import java.sql.*;

public class QueryFromDB {
/**
* 用表的列名和表名构造select语句
* @param colName
* @param tableName
* @return
* @throws ArrayIndexOutOfBoundsException
*/
public String ConstructQuery(String tableName, String[] colName)
throws ArrayIndexOutOfBoundsException {
String str = "select ";
for (int i = 0; i < colName.length; i++) {
str += "/"" + colName[i] + "/"" + ",";
}
str.replace(str.charAt(str.length() - 1), ' ');
str += "from " +"/"" +tableName+"/"";
return str;
}
/**
* 具体执行上述语句
* @param col
* @param tableName
*/
public ResultSet exeQuery(String tableName, String[] col) {
ResultSet rs=null;
try {
rs =
ConnectToDB.getStmt().executeQuery(
this.ConstructQuery(tableName,col));

} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return rs;
}
}

下面是数据库连接的封装类:
import java.sql.*;
/**
* @author admin1
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ConnectToDB {
public ConnectToDB() {
}
public static Connection getConn() throws ClassNotFoundException, SQLException {

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connt =
DriverManager.getConnection(
"jdbc:oracle:thin:@10.10.11.144:1521:edu",
"study1",
"study1");
return connt;
}

public static Statement getStmt() throws SQLException, ClassNotFoundException {

Statement stmt =ConnectToDB.getConn().createStatement();
ConnectToDB.getConn().close();
return stmt;
}

}
然后是主类的调用方法了:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
RegForm regForm = (RegForm) form;
RegEntity newEntity = regForm.getRegEntity();
QueryFromDB qfdb = new QueryFromDB();
String[] col = { "ENAME", "SEX", "IDCRDNU", "CURRADDR", "MPHONE" };
String tbName = "CUSTOMINFO_XWW";
ResultSet rs = qfdb.exeQuery(tbName, col);
while (rs.next()) {
newEntity.setName(rs.getString("ENAME"));
newEntity.setSex(rs.getString("SEX"));
newEntity.setIdCard(rs.getString("IDCRDNU"));
newEntity.setPhoneNum(rs.getString("MPHONE"));
newEntity.setAddr(rs.getString("CURRADDR"));
}
regForm.setRegEntity(newEntity);
return mapping.findForward("go");
}
可是运行老出现如标题那样的错误!清高手指点!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值