簡體   English   中英

將MySQL數據庫中的1個列表圖像加載到列表JSP頁面中

[英]Load 1 list image from MySQL database into list JSP Page

我的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,之后我選擇查詢從表圖像中獲取一些屬性。 之后,我想讓propeerties進入我的代碼,以顯示圖像到頁面。

在這里我有一個css文件,一個js文件,一些圖片在路徑圖像/ ...

謝謝!

首先,在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM