【问题标题】:Load 1 list image from MySQL database into list JSP Page将 MySQL 数据库中的 1 个列表图像加载到列表 JSP 页面中
【发布时间】:2023-03-09 15:41:01
【问题描述】:

我的 jsp 页面有问题。 首先,我创建了一个数据库,表图像具有一些属性 imageID、imageTitle、imageURL、imageStyle 继续我创建一个新的jsp页面 这是代码:

<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            try {
            String _URL = "jdbc:mysql://localhost/slider";
            String _USER = "root";
            String _PASS = "password";
            java.sql.Connection connection = null;

            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            connection = DriverManager.getConnection(_URL, _USER, _PASS);
            //if(!connection.isClosed())
                 //out.println("Successfully connected to " + "MySQL server using TCP/IP...");
            //connection.close();

            int img_id = 1;
            ResultSet rs = null;
            PreparedStatement pstmt = null;
            String strslide = "";
            strslide += "<div class=\"panel\">";

            strslide += "<div class=\"container\"><div class=\"wt-rotator\">";
            strslide +="<a href=\"#\"></a>";
            strslide += "<div class=\"desc\"></div><div class=\"preloader\"></div><div class=\"c-panel\"><div class=\"buttons\">";
            strslide+="<div class=\"prev-btn\"></div>";
            strslide+="<div class=\"play-btn\"></div>";
            strslide+="<div class=\"next-btn\"></div>";

            strslide+="</div>";

            strslide +="<div class=\"thumbnails\">";
            strslide += "<ul>"; 
            try {
                pstmt = connection.prepareStatement("select * from image where id = @id");
                pstmt.setInt(1, img_id);
                rs = pstmt.executeQuery();
                if(rs.next()) {
                   // some code at here
                }
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
        }catch(Exception ex){
            out.println("Unable to connect to database"+ex);
        } 

        %>
    </body>
</html>

我不知道如何使用 jsp 代码将图像显示到页面中,我编写了一些理想的代码。我创建到 mysql 的连接,之后我选择查询以从表图像中获取一些属性。在我想在我的代码中提供属性以在页面中显示图像之后。

在这里我有一个 css 文件,一个 js 文件,一些图片在路径 images/...

谢谢!

【问题讨论】:

    标签: java jquery html mysql jsp


    【解决方案1】:

    首先,在 jsp 中编写 java 代码是一种不好的做法。您必须创建单独的类 SliderDao 并将所有 jdbc 代码放在那里。

    创建 PreparedStatement 不像:

    pstmt = connection.prepareStatement("select * from image where id = @id");
    

    应该是这样的

    pstmt = connection.prepareStatement("select * from image where id = ?");
    pstmt.setInt(1, img_id);
    rs = pstmt.executeQuery();
    

    阅读本教程http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html#create_ps

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2013-04-12
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多