longlyheart
2007-04-02 10:56:48 UTC
小弟我寫了一個搜尋網頁,想要請問:
如何將寫在網頁裡面的資料庫移出來變成一個Bean??
以下是我的程式碼:
<%@ page contentType = "text/html; charset=utf-8" %>
<%@ page language = "java" %>
<%@ page import = "java.sql.*" %>
<html>
<head>
<title>show</title>
</head>
<body>
<%
//宣告變數
int count = 0;
String error = null;
//設定資料庫參數
String driver="com.mysql.jdbc.Driver";
String userName="帳號";
String password="密碼";
String url="jdbc:mysql://localhost:3306/資料庫名稱";
//接收資料
String dbname = request.getParameter("dbname");
//SQL指令
String comSQL = "SELECT * FROM articledb WHERE articlename like'%"+dbname+"%';";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,userName,password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(comSQL);
while(rs.next()){
int sn = rs.getInt("articlesn");
String pi = rs.getString("picture");
String na = rs.getString("articlename");
String slink = "buytwo.jsp?iid="+sn;
count++;
out.println("<a href = " + slink + "><img src = " + pi + "></a><br>");
out.println("<a href = " + slink + ">" + na + "</a><br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se){
error = se.toString();
}
catch(Exception e){
error = e.toString();
}
if(count==0){
out.println("非常抱歉,我們沒有您所需要的商品。");
}
%>
</body>
大約就是以上try裡面的東西,希望可以改成由Bean匯入
但是Bean不支援System.out.println這種東西....
所以寫了幾個Bean都沒辦法達到同樣的效果,請各位大大指導>"<
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.96.194
如何將寫在網頁裡面的資料庫移出來變成一個Bean??
以下是我的程式碼:
<%@ page contentType = "text/html; charset=utf-8" %>
<%@ page language = "java" %>
<%@ page import = "java.sql.*" %>
<html>
<head>
<title>show</title>
</head>
<body>
<%
//宣告變數
int count = 0;
String error = null;
//設定資料庫參數
String driver="com.mysql.jdbc.Driver";
String userName="帳號";
String password="密碼";
String url="jdbc:mysql://localhost:3306/資料庫名稱";
//接收資料
String dbname = request.getParameter("dbname");
//SQL指令
String comSQL = "SELECT * FROM articledb WHERE articlename like'%"+dbname+"%';";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,userName,password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(comSQL);
while(rs.next()){
int sn = rs.getInt("articlesn");
String pi = rs.getString("picture");
String na = rs.getString("articlename");
String slink = "buytwo.jsp?iid="+sn;
count++;
out.println("<a href = " + slink + "><img src = " + pi + "></a><br>");
out.println("<a href = " + slink + ">" + na + "</a><br>");
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se){
error = se.toString();
}
catch(Exception e){
error = e.toString();
}
if(count==0){
out.println("非常抱歉,我們沒有您所需要的商品。");
}
%>
</body>
大約就是以上try裡面的東西,希望可以改成由Bean匯入
但是Bean不支援System.out.println這種東西....
所以寫了幾個Bean都沒辦法達到同樣的效果,請各位大大指導>"<
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.96.194