简体   繁体   English

为什么我的JDBC(读取)查询在我的JSP页面上不起作用?

[英]Why won't my JDBC (read) query work on my JSP page?

I wrote a .jsp page as part of a larger databse project. 我写了一个.jsp页面,作为一个更大的数据库项目的一部分。 The goal is to have a series of .jsp pages which use java and html to display data from a mySQL database. 目标是拥有一系列.jsp页面,这些页面使用java和html来显示来自mySQL数据库的数据。 I am having an issue with JDBC queries, in particular using prepared statements to select a car model from my inventory table. 我在JDBC查询时遇到问题,尤其是使用准备好的语句从库存表中选择汽车模型时。 I have coded it so that a the car with id = 3 will return the corresponding car model, and then print it on the screen. 我已对其进行编码,以便ID = 3的汽车将返回相应的汽车模型,然后将其打印在屏幕上。 After I run the code, there is no output on the screen, and so the issue, probably, lies in my query or the connection setup before it, I'm just not sure where. 运行代码后,屏幕上没有输出,因此问题可能出在我的查询或之前的连接设置上,我不确定在哪里。

<%-- 
Document   : content
Created on : May 6, 2015, 5:04:20 PM
Author     : Christopher
--%>

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

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>content</title>
    </head>
    <body>
        <h1>Welcome!</h1>
    <%!

public class Template {

public Template(){

    try{

        Class.forName("com.mysql.jdbc.driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/itmd4515?zeroDateTimeBehavior=convertToNull"
        ,"itmd4515","itmd4515");
        PreparedStatement ppst = con.prepareStatement("select * from inventory where id = 3");
        ResultSet rs = ppst.executeQuery();
        rs.next();
        String carModel = rs.getString("carModel");
        rs.close();
        con.close();

        System.out.println(carModel);

    }catch(SQLException ex){
        System.err.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Template.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}


    %>
    <br><br>
    <%= new java.util.Date() %>
</body>

库存 The inventory table 库存表

I'm confident that the problem is somewhere in the above code, as there are no errors when I run the code; 我确信问题出在上面的代码中,因为在我运行代码时没有错误。 it simply doesn't display the car model. 它根本不显示汽车型号。 Also, the database is properly configured and connected, so that likely isn't the issue either. 另外,数据库已正确配置和连接,因此也可能不是问题。

I apologize if the issue is an obvious one, I'm still getting a hang of JDBC and .jsp. 如果问题很明显,我深表歉意,但我仍然对JDBC和.jsp感到困惑。 This is a maven web app done in Netbeans. 这是在Netbeans中完成的Maven Web应用程序。

日志

The latest system messages 最新系统消息

After modifying the code to output the SQLException and ClassNotFoundException as I received the following output in my browser: com.mysql.jdbc.driver 修改代码以输出SQLException和ClassNotFoundException之后,我在浏览器中收到以下输出:com.mysql.jdbc.driver

Your code declares Template class and constructor for this class. 您的代码声明了Template类和该类的构造函数。 Why would it be executed if class is not even instantiated? 如果什至没有实例化类,为什么还要执行它呢?

Apart from that this is a very weird way of writing JSP pages. 除此之外,这是编写JSP页面的一种非常奇怪的方式。 You need to read some decent book about writing simple JSP pages: smth like "Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)" - which is very old but give a clue to beginners where and how to write plain simple JSP/Servlets 您需要阅读一些有关编写简单JSP页面的体面的书:诸如“ Core Servlets and Javaserver Pages:Core Technologies,第1卷(第2版)”之类的书-虽然年代久远,但是却为初学者提供了在何处以及如何编写纯文本的线索简单的JSP / Servlet

Replace your System.out.println(carModel) to out.println(carModel) 将您的System.out.println(carModel)替换为out.println(carModel)

Have a test~ 考试〜

Here is a post for you Why do we write out.println() in jsp instead of System.out.println()? 这是给您的帖子为什么我们用jsp而不是System.out.println()写出out.println()?

I never write class and method in jsp. 我从来没有在jsp中编写类和方法。 If you wanna test the jdbc clause work or not, just write a simple java file, use a main method. 如果您想测试jdbc子句是否有效,只需编写一个简单的Java文件,使用main方法即可。 Like below 像下面

/**
 * Copyright (c) 2014
 * Tanry Electronic Technology Co., Ltd.
 * ChangSha, China
 * 
 * All Rights Reserved.
 * 
 * First created on May 7, 2015 by liyzh
 * Last edited on May 7, 2015 by liyzh
 * 
 * 说明: TODO
 */
package com.tanry.business.module.lc.request;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/itmd4515?zeroDateTimeBehavior=convertToNull", "itmd4515", "itmd4515");
            PreparedStatement ppst = con.prepareStatement("select * from inventory where id = 3");
            ResultSet rs = ppst.executeQuery();
            while (rs.next()) {
                String carModel = rs.getString("carModel");
                System.out.println(carModel);
            }
            rs.close();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            // Logger.getLogger(Template.class.getName()).log(Level.SEVERE,
            // null, ex);
        }
    }
}

Using an IDE like eclipse, you can debug your code really easily. 使用像eclipse这样的IDE,您可以非常轻松地调试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM