jsp页面中的sql异常 [英] sql exception in jsp page

查看:85
本文介绍了jsp页面中的sql异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为JSP代码为:

 <%@  页面    import   =  org.apache.tomcat.util.net.SSLUtil %>  
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN
http://www.w3.org/TR/html4/loose.dtd >

<%@ page import = java.sql。* %>
<%@ page import = java.io。* %>

< html >
< head >
< title > 与mysql数据库的连接< /标题 >
< / head >
< ; 正文 >
< h1 > 连接状态< ; / h1 >
<%
尝试 {
字符串 connectionURL = < span class =code-string> jdbc:mysql://dbno2.cfghe6zvdgnoq7r.ap-southeast-2.rds.amazonaws.com:3306 / mydb?用户= aghil&安培;密码=密码1\" ;
连接连接= null ;
语句stmt = null ;
Class.forName( com.mysql.jdbc.Driver)。newInstance() ;
connection = DriverManager.getConnection(connectionURL);
if (!connection.isClosed()){
stmt = connection.createStatement();
}
out .println( 使用TCP / IP成功连接到MySQL服务器... \ n);
字符串 QueryString = 从mydb中选择用户名.userdetails WHERE username ='anil'和password ='password';
ResultSet rs;
字符串 sre = null ;
try {
PreparedStatement statement = connection.prepareStatement(QueryString);
rs = statement.executeQuery();
sre = rs.getString( 1 );
out .println( \ n + sre);
} catch (例外e){
out .println( \ n + e.toString());
}
connection.close();
} catch (例外情况){
out .println( 无法连接到数据库。 + ex);
}
%>
< / body >
< / html >







及其在浏览器中的输出如下:



< html>< head>

< title>与mysql数据库连接< / title>

< / head>

< body>

连接状态



使用TCP /
成功连接到MySQL服务器b $ b

java.sql.SQLException:在开始结果集之前>< / html>



为什么会这样?怎么办?

解决方案

PreparedStatement statement = connection.prepareStatement(QueryString);

rs = statement.executeQuery();

if(rs.next())

sre = rs.getString(1);

out.println(\ n+ sre) ;

I hava Jsp code as:

<%@page import="org.apache.tomcat.util.net.SSLUtil"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>

<html>
    <head>
        <title>Connection with mysql database</title>
    </head>
    <body>
        <h1>Connection status </h1>
        <%
            try {
                String connectionURL = "jdbc:mysql://dbno2.cfghe6zvdgnoq7r.ap-southeast-2.rds.amazonaws.com:3306/mydb?user=aghil&password=password1";
                Connection connection = null;
                Statement stmt = null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(connectionURL);
                if (!connection.isClosed()) {
                    stmt = connection.createStatement();
                }
                out.println("Successfully connected to MySQL server using TCP/IP...\n");
                String QueryString = "select username from mydb.userdetails WHERE username ='anil' and password='password'";
                ResultSet rs;
                String sre = null;
                try {
                    PreparedStatement statement = connection.prepareStatement(QueryString);
                    rs = statement.executeQuery();
                    sre = rs.getString(1);
                    out.println("\n" + sre);
                } catch (Exception e) {
                    out.println("\n" + e.toString());
                }
                connection.close();
            } catch (Exception ex) {
                out.println("Unable to connect to database." + ex);
            }
        %>
    </body>
</html>




and its output in browser is as follows :

<html><head>
<title>Connection with mysql database</title>
</head>
<body>

Connection status


Successfully connected to MySQL server using TCP/

java.sql.SQLException: Before start of result set></html>

why this? what to do?

解决方案

PreparedStatement statement = connection.prepareStatement(QueryString);
rs = statement.executeQuery();
if(rs.next())
sre = rs.getString(1);
out.println("\n" + sre);


这篇关于jsp页面中的sql异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆