本文目錄一覽:
- 1、JSP提取SQL資料庫數據問題
- 2、jsp連接sql資料庫,並用jsp把數據導入資料庫中
- 3、jsp中select值如何從資料庫中取出然後,傳到另一個jsp頁面?
- 4、怎麼從資料庫中提取數據,在jsp頁面顯示
- 5、jsp獲取資料庫中的數據
- 6、用jsp、java實現上傳圖片,保存到資料庫,從資料庫中提取,顯示到頁面 這四步 我想問第一步,怎麼上傳圖片
JSP提取SQL資料庫數據問題
首先確認,你確實能在jsp頁面上寫出編號ID,當點擊這個連接的時候傳入一個servlet,(建議使用servlet的service方法,這樣可以接受post和get的兩種方式的請求),然後在方法中寫到String id = request.getParameter(“id”);
這樣就可以獲取id的值了
你在寫jsp超連接的時候,要寫成這樣的a href=”servlet/id=%=id%”這樣就可以了
jsp連接sql資料庫,並用jsp把數據導入資料庫中
JSP連接SQL資料庫實現查找(支持模糊查找,查找年齡段),插入信息實例
h2學生信息查詢/h2
form method=”POST” action=”Name.jsp”
h4按姓名查找(支持模糊查詢)/h4
table bgcolor=”#CCCCCC”
tr
td查找姓名/td
tdinput type=”text” name=”name” size=”15″ //td
tdinput type=”submit” value=”查找”/td
/tr
/table
/form
br/
form method=”POST” action=”Age.jsp”
h4按年齡查找/h4
table border=”1″ bgcolor=”#CCCCCC”
tr
td查找年齡/td
tdinput type=”text” name=”agemin” size=”5″ //td
td到/td
tdinput type=”text” name=”agemax” size=”5″ //td
tdinput type=”submit” value=”查找”/td
/tr
/table
/form
form action=”Insert.jsp” method=”POST”
h4插入信息到表中/h4
table border=”1″ bgcolor=”#cccccc”
tr
td姓名/td
tdinput type=”text” name=”name” //td
/tr
tr
td性別/td
tdinput type=”text” name=”sex” //td
/tr
tr
td年齡/td
tdinput type=”text” name=”age” //td
/tr
tr
td系別/td
tdinput type=”text” name=”dept” //td
/tr
tr
tdinput type=”submit” value=”插入” //td
tdinput type=”reset” value=”重置” //td
/tr
/table
/form
/center
/body
/html
jsp中select值如何從資料庫中取出然後,傳到另一個jsp頁面?
jsp 有九大內置對象,常用的有session、request、response…..你可以用這些內置對象傳遞所查詢的對象到另一個頁面,當然這是最最基本的,而如果你用了struts2框架,配置好了後,就可以很簡單的將查詢出的數據顯示到jsp頁面上了。
具體做法。1、將查詢的數據用list封裝。2、然後將list 存放到一個request中。3、在另一個頁面中讀取顯示即可
怎麼從資料庫中提取數據,在jsp頁面顯示
在資料庫提取部分數據,在JSP上顯示的做法如下:
思路:1、創建db連接 2、創建statement 3、執行查詢 4、遍歷結果並展示
完整代碼如下:
span style=”font-size:12px;”span style=”font-size:14px;”%@ page language=”java” import=”java.sql.*,java.io.*,java.util.*”%
%@ page contentType=”text/html;charset=utf-8″%
html
head
style type=”text/css”
table {
border: 2px #CCCCCC solid;
width: 360px;
}
td,th {
height: 30px;
border: #CCCCCC 1px solid;
}
/style
/head
body
%
//驅動程序名
String driverName = “com.mysql.jdbc.Driver”;
//資料庫用戶名
String userName = “root”;
//密碼
String userPasswd = “szy”;
//資料庫名
String dbName = “studentmanage”;
//表名
String tableName = “student”;
//聯結字元串
String url = “jdbc:mysql://localhost:3306/” + dbName + “?user=”
+ userName + “password=” + userPasswd;
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql = “SELECT * FROM ” + tableName;
ResultSet rs = statement.executeQuery(sql);
%
br
br
table align=”center”
tr
th
%
out.print(“學號”);
%
/th
th
%
out.print(“姓名”);
%
/th
th
%
out.print(“專業”);
%
/th
th
%
out.print(“班級”);
%
/th
/tr
%
while (rs.next()) {
%
tr
td
%
out.print(rs.getString(1));
%
/td
td
%
out.print(rs.getString(2));
%
/td
td
%
out.print(rs.getString(3));
%
/td
td
%
out.print(rs.getString(4));
%
/td
/tr
%
}
%
/table
div align=”center”
br br br
%
out.print(“數據查詢成功,恭喜你”);
%
/div
%
rs.close();
statement.close();
connection.close();
%
/body
/html/spanspan style=”font-size:24px;color: rgb(255, 0, 0);”
/span/span
jsp獲取資料庫中的數據
%
//JSP頁面直接訪問資料庫
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName(“JDBC驅動”);
conn = DriverManager.getConnection(“url”, “username”, “password”);
stmt = conn.createStatement();
rs = stmt.executeQuery(“select factor, ratio from 表名 where id=1”);
while(rs.next()){
String factor = rs.getString(“factor”);
String ratio = rs.getString(“ratio”);
%
factor :%=factor %
ratio :%=ratio %
%
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
%
修改 驅動、url、username、password、表名、欄位名成你應用的相應數據,然後將這些代碼加入到你的jsp頁面,就可以在jsp頁面直接讀取到資料庫中的對應表指定欄位的數據了,祝你好運!
用jsp、java實現上傳圖片,保存到資料庫,從資料庫中提取,顯示到頁面 這四步 我想問第一步,怎麼上傳圖片
用jspSmartUpload組件來實現,用jsp+servlet在Servlet里實現的代碼:
PrintWriter out = response.getWriter();
int count = 0;
// 實例化上傳控制項對象
SmartUpload su = new SmartUpload();
// 初始化操作
su.initialize(config, request, response);
// 設置上傳文件最大位元組數
su.setTotalMaxFileSize(100000);
//
try {
//禁止上傳指定擴展名的文件
su.setDeniedFilesList(“ext,bat,jsp”);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
// 上傳文件到伺服器
su.upload();
File fileup = new File(request.getRealPath(“upload”));
if(!fileup.exists()){
// 創建目錄
fileup.mkdir();
}
// 處理多個文件的上傳
for(int i = 0;i su.getFiles().getCount();i++){
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if(!file.isMissing()){ // 如果文件有效
// 保存文件到指定上傳目錄
file.saveAs(“/upload/new.”+file.getFileExt(), su.SAVE_VIRTUAL);
count = su.save(“/upload”);
}
}
} catch (SmartUploadException e) {
e.printStackTrace();
}
out.println(count +”file(s) uploaded”);
如果你對這個上傳組件不了解,最好是先去查查用法。。。
如果對您有幫助,請記得採納為滿意答案,謝謝!祝您生活愉快!
vaela
原創文章,作者:HY1VO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127558.html