当前位置: 代码迷 >> Java Web开发 >> 连接数据库 rs.next() 报错,该如何解决
  详细解决方案

连接数据库 rs.next() 报错,该如何解决

热度:1160   发布时间:2016-04-17 00:22:28.0
连接数据库 rs.next() 报错
RT:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%@page import="pc.DBHelper"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>My JSP 'newsList.jsp' starting page</title>
  </head>
  <jsp:useBean id="Conn" class="pc.DBHelper"/>
  <body>
  
<table width="200" cellspacing="1" cellpadding="1">
<tr>
<td>ID</td>
<td>名称</td>
</tr>
<% ResultSet rs1 = Conn.getResultSet("select * from news quot"); %>
<% while(rs1.next())
{%>
<tr>
<td><%= rs1.getInt("id")%></td>
<td><%= rs1.getString("content")%></td>
</tr>
<%} %>
</table>


  </body>
</html>

页面

package pc;
import java.sql.*;
public class DBHelper 
{
static final String DriverName="com.mysql.jdbc.Driver"; //driver
static final String dbURL="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK";

public static Connection getConnection()
{
Connection conn = null;
try
{
Class.forName(DriverName);
conn = DriverManager.getConnection(dbURL,"root","root"); //conn object
}
catch (Exception e){}
return conn;
}

public static void Close(Connection conn)
{
try //conn close
{
if(conn!=null)
conn.close();
}
catch(Exception e){}
}

public void Close(ResultSet rs) //resultset close
{
try
{
if(rs!=null)
rs.close();
}
catch(Exception e){}
}

public void Close(Statement operation) //statement close
{
try
{
if(operation!=null)
operation.close();
}
catch(Exception e){}
}

public ResultSet getResultSet(String st)
{
try
{
Statement operation1 = DBHelper.getConnection().createStatement();
ResultSet rs1 = operation1.executeQuery(st);
return rs1;
}
catch (Exception e) 
{}
return null;
}

public int getexecuteUpdate(String st)
{
int i = -1;
try
{
Statement stmt = (DBHelper.getConnection()).createStatement();
i = stmt.executeUpdate(st);
return i;
}
catch (Exception e) 
{e.printStackTrace();}
return i;
}






}

.java

为什么 rs.next()报错?


------解决方案--------------------
Java code
public final class JdbcUtilsSing {    private String url = "jdbc:mysql://localhost:3306/jdbc";    private String user = "root";    private String password = "";    // private static JdbcUtilsSing instance = new JdbcUtilsSing();    private static JdbcUtilsSing instance = null;    private JdbcUtilsSing() {    }    public static JdbcUtilsSing getInstance() {        if (instance == null) {            synchronized (JdbcUtilsSing.class) {                if (instance == null) {                    instance = new JdbcUtilsSing();                }            }        }        return instance;    }    static {        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            throw new ExceptionInInitializerError(e);        }    }    public Connection getConnection() throws SQLException {        return DriverManager.getConnection(url, user, password);    }    public void free(ResultSet rs, Statement st, Connection conn) {        try {            if (rs != null)                rs.close();        } catch (SQLException e) {            e.printStackTrace();        } finally {            try {                if (st != null)                    st.close();            } catch (SQLException e) {                e.printStackTrace();            } finally {                if (conn != null)                    try {                        conn.close();                    } catch (SQLException e) {                        e.printStackTrace();                    }            }        }    }}
  相关解决方案