【问题标题】:JSP/MySQL(JDBC) - Select statement is giving empty results [closed]JSP/MySQL(JDBC) - Select 语句给出空结果[关闭]
【发布时间】:2017-04-08 15:59:57
【问题描述】:

JDBC-MySQL 驱动程序已正确安装且连接正常。

我已插入创建的数据库、表、插入的行,并从视图数据中执行了 select 语句。那些工作得很好。但是在这段代码中,只有 INSERT 语句在起作用,并且在 SELECT 语句没有的地方添加了结果。 在互联网上搜索了几个小时后,我也不知道。

这里是代码-

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%

    String connectionURL = "jdbc:mysql://localhost:3306/users";
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "pass");
    if (!connection.isClosed()) {
        out.println("Connection Established");

        PreparedStatement preparedStmt = connection.prepareStatement("INSERT INTO users.user_details (`Email`, `Name`, `Password`)  VALUES ('ram@gmail.com', 'Ram Das', '12345678')");
        preparedStmt.execute();
        String sql="select * from user_details";
        PreparedStatement statement=connection.prepareStatement(sql);
        ResultSet rs=statement.executeQuery();
        if(rs.next())
        {
            System.out.println(rs.getString(1));
        } 
    } 
    connection.close();
%>

【问题讨论】:

  • 您使用了System.out 而不是 out。 ist 是否打印在控制台中?
  • @SilverNak,哈哈,谢谢,我只是忽略了它。我的错。

标签: java mysql database jsp jdbc


【解决方案1】:
   While(rs.next())
    {
        System.out.println(rs.getString("Email"));
    } 

用while代替if。

如果您使用 out.println(),您将在浏览器中看到输出,如果您使用 System.out.println(),您将在 ide 的输出窗口中看到输出。

【讨论】:

  • 是的,我现在明白了。并且 +1 用于提醒使用“while”,因为在“if”的情况下,只会显示一个结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 2011-02-12
  • 2013-05-29
  • 2013-06-22
相关资源
最近更新 更多