JSP连接数据库问题
如下代码
Database.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DataBase
{
protected String jdbcStr;
protected String conStr;
protected Statement stmt;
protected String sql;
protected Connection conn;
protected ResultSet rs;
protected boolean isTranStarted;
public DataBase()
{
isTranStarted = false;
}
public DataBase(String jdbcStr,String conStr)
{
isTranStarted = false;
}
protected void connect(String jdbcStr,String conStr) throws SQLException
{
this.jdbcStr =jdbcStr;
this.conStr =conStr;
try{
System.out.println("JDBC Driver:"+this.jdbcStr);
Class.forName(this.jdbcStr);
}catch (Exception e){
System.out.println("Load JDBC Driver Error: "+e.getMessage());
throw new SQLException("Load JDBC Driver Error: " + e.getMessage());
}
try{
conn =DriverManager.getConnection(conStr);
}catch(Exception e){
System.out.println("Load JDBC Connection Error: "+e.getMessage());
throw new SQLException("Load JDBC Connection Error: " + e.getMessage());
}
try{
stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception e) {
System.out.println("Load JDBC Statement Error: "+e.getMessage());
throw new SQLException("Load JDBC Statement Error: " + e.getMessage());
}
}
public void startTransaction() throws SQLException
{
conn.setAutoCommit(false);
isTranStarted = true;
}
public void commit() throws SQLException
{
if (isTranStarted)
{
conn.commit();
isTranStarted = false;
}
}
public void rollback()
{
if (isTranStarted)
{
try{
conn.rollback();
}catch (Exception e){
System.out.println("rollback error: " +e.getMessage());
}
isTranStarted = false;
}
}
public ResultSet executeQuery(String sql)
{
try
{
rs =stmt.executeQuery(sql);
}
catch(Exception e)
{
System.out.println("executeQuery error: " + e.getMessage());
return null;
}
return rs;
}
public int executeUpdate(String sql) throws SQLException
{
return stmt.executeUpdate(sql);
}
public void close()
{
if (isTranStarted ==true)
{
isTranStarted = false;
try{
conn.rollback();
}catch (Exception e){
System.out.println("rollback error: "+e.getMessage());
}
}
if(rs !=null)
try{
rs.close();
}catch (Exception e){
System.out.println("resultset close error: "+e.getMessage());
}
if(stmt !=null)
try{
stmt.close();
}catch (Exception e){
System.out.println("statement close error: "+e.getMessage());
}
if(conn !=null)
try{
conn.close();
}catch (Exception e){
System.out.println("connection close error: "+e.getMessage());
}
}
}
myoa.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class myoa extends DataBase
{
public myoa()
{
String host="localhost";
String databasename="oa";
String username="sa";
String password="123";
String jdbcStr;
String conStr;
String debug="yes";
jdbcStr="com.microsoft.jdbc.sqlserver.SQLServerDriver";
conStr ="jdbc:microsoft:sqlserver://"+host+":1433;User="+username+";Password="+
password+";DatabaseName="+databasename;
if(debug.equals("yes"))
{
System.out.println(conStr);
}
try
{
super.connect(jdbcStr,conStr);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args)
{
myoa oadb =new myoa();
String sql="Insert into test2 values ('test')";
try
{
int ret =oadb.executeUpdate(sql);
System.out.println("insert result: "+ret);
}catch (Exception e){
oadb.rollback();
System.out.println("update error: "+e.getMessage());
}
myoa.close();
}
}
checklogin.jsp
<%@ page language="java" import="java.sql.*,com.myoa.db" contentType="text/html;charset=gb2312"%>
<jsp:useBean id="oadb" scope ="page" class="com.myoa.db"/>
<%
String loginID = new String(request.getParameter("LoginID").getBytes("ISO-8859-1"));
String password = new String(request.getParameter("Password").getBytes("ISO-8859-1"));
String adminCheck ="";
String strsql = "select count(*) from User_Information";
ResultSet rs = oadb.executeQuery(strsql);
rs.next();
if (rs.getInt(1)<1)
{
if (loginID.equals("admin") && password.equals("admin"))
{
...
...
}
JSP运行时总是提示红色的地方出错,看过原因是返回的记录集是空的,但数据表是有数据,不知道错在哪...