Servlet 代码中的结果集处理错误

发布于 2024-12-25 03:26:30 字数 1918 浏览 0 评论 0原文

我在 servlet 中编写了一段代码用于登录检查,如果我没有注释掉 String s4 =,我不知道为什么会收到类似 java.sql.SQLException: No data found 的错误rs.getString(1)out.println(s4) 行如果我注释掉这行,我没有收到任何错误。

为什么我会收到这样的错误?我找不到答案。

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class login extends HttpServlet {

    Connection conn;
    Statement stmt;
    ResultSet rs;
    String s = "";

    public void init() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("Jdbc:Odbc:edsn");
            s = "Your information is connected ......";
        } catch (Exception e) {
            s = "Exception 1....." + e.getMessage();
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        out.println(s);
        try {

            String ID = req.getParameter("T1");
            String query = "select * from user_db ";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            out.println("user" + " " + "pass");
            while (rs.next()) {

                try {
                    if ((rs.getString(1)).equals(ID)) {
                        String s4 = rs.getString(1);
                        out.println(s4);

                        out.println("<html><body><h> login Pass.....:(</h></body></html>");

                    }

                } catch (Exception e) {
                    out.println(e);
                }
            }

        } catch (Exception e) {
            out.println("Unable To Show the info... . . ." + e.getMessage());
        }

    }
}

I write a code in servlet for login checking I don't know why I get an error like java.sql.SQLException: No data found, if I had not commented out the String s4 = rs.getString(1) and out.println(s4) line if I commented out this lines I did not get any error.

Why do I get an error like this? I cannot find out the answer.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class login extends HttpServlet {

    Connection conn;
    Statement stmt;
    ResultSet rs;
    String s = "";

    public void init() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("Jdbc:Odbc:edsn");
            s = "Your information is connected ......";
        } catch (Exception e) {
            s = "Exception 1....." + e.getMessage();
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        out.println(s);
        try {

            String ID = req.getParameter("T1");
            String query = "select * from user_db ";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            out.println("user" + " " + "pass");
            while (rs.next()) {

                try {
                    if ((rs.getString(1)).equals(ID)) {
                        String s4 = rs.getString(1);
                        out.println(s4);

                        out.println("<html><body><h> login Pass.....:(</h></body></html>");

                    }

                } catch (Exception e) {
                    out.println(e);
                }
            }

        } catch (Exception e) {
            out.println("Unable To Show the info... . . ." + e.getMessage());
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2025-01-01 03:26:30

为什么要写这样的代码?翻遍整张桌子真是太浪费了……
仅IO...
为什么不改变这一点:

ResultSet rs = null;
PreparedStatement st = null;
try {...
String ID = req.getParameter("T1"); 
            String query = "select 1 from user_db where col_name = ?"; 
            st = conn.prepareStatement(query);
            st.setString(1, ID);  
            rs = st.executeQuery();
if (rs.next()) {
  out.println(ID); 
  out.println("<html><body><h> login Pass.....:(</h></body></html>"); 
}
..

} finally {
if (rs != null) try { rs.close();}catch (Exception e) {}
if (st != null) try { st.close();}catch (Exception e) {}
}

注意准备好的语句被缓存并且更适合频繁使用

你让数据库做它擅长的事情 - 搜索数据

select 1 而不是 select * 不会带回你并不真正需要的数据

jdbc 工作越多列越多以及您返回的一般数据,因此仅获取您所返回的数据
需要

并添加一个finally块以始终正确关闭数据库连接

Why write the code like this ? It's very wasteful going over the whole table...
The IO alone...
Why not change to this:

ResultSet rs = null;
PreparedStatement st = null;
try {...
String ID = req.getParameter("T1"); 
            String query = "select 1 from user_db where col_name = ?"; 
            st = conn.prepareStatement(query);
            st.setString(1, ID);  
            rs = st.executeQuery();
if (rs.next()) {
  out.println(ID); 
  out.println("<html><body><h> login Pass.....:(</h></body></html>"); 
}
..

} finally {
if (rs != null) try { rs.close();}catch (Exception e) {}
if (st != null) try { st.close();}catch (Exception e) {}
}

notice prepared statements are cached and better for frequent use

you let the db do what its good at - search the data

select 1 instead of select * does not bring back data you dont really need

jdbc works harder the more columns and data in general you return, so only get what you
need

and add a finally block to always close your db connections properly

笑,眼淚并存 2025-01-01 03:26:30

对 Connection、Statement 或 ResultSet 的调用方法取决于您加载的 JDBC 驱动程序。 ResultSet 的所有值都可以在查询后立即设置,也可以根据需要从数据库中检索,具体取决于驱动程序的实现。

JdbcOdbcDriver 在第二次调用 getString 后抛出 SQLException。这可以通过将值存储在字符串中而不是进行多次调用或切换到不同的驱动程序来解决。

Calling methods on Connection, Statement, or ResultSet depend on which JDBC driver you've loaded. All the values of the ResultSet could be set as soon as the query is made, or they could be retrieved from the database as they're needed, depending on the implementation of the driver.

The JdbcOdbcDriver throws an SQLException after calling getString for a second time. This can be worked around be storing the values in Strings instead of making multiple calls, or by switching to a different driver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文