當前位置:首頁 » 操作系統 » jsp數據保存到資料庫

jsp數據保存到資料庫

發布時間: 2022-05-16 04:19:21

1. JSP的table表單如何保存到資料庫

這些值是填到了表單里的,
點保存就相當於提交表單的submin那個東西
提交到用於計算和保存的jsp頁面之後,那個頁面接收表單傳過來的值,然後計算得出結果。

剩下的就是jsp的資料庫操作部分了。獲取資料庫連接,創建語句對象,執行sql語句,返回結果。

2. 如何把jsp中的數據提交到mysql資料庫中呢

可以通過jsp 的form 提交到後台 servlet 等 然後調用 hibernate 存到資料庫
當然這里的資料庫你想是啥都OK
MVC模式 SSH 框架 V 就是JSP 的

3. JSP,選擇型(select-option)的數據如何保存到資料庫表中

首先在JSP頁面的資料庫處理部分使用:try {statement(.....); }catch(SQLException e) { 1、像上面的語句中,我不知道是復制過來的還是寫錯了: String sql=insert into tb_member values('+income+'); 很明顯右邊少了一個引號: String sql=insert into tb_member values('+income+'); 2、拋出異常中可能說明是違反主鍵約束:查看資料庫表,將次主鍵約束暫時刪除。 3、拋出異常可能說明字元串被截斷,說明你定義的char類型太短,一般向這樣的欄位保持定義在varchar(20)左右。 4、本來還有可能是類型不匹配或指定參數太多或不夠等,但就你題目中描述,只有一個欄位就能出現這樣的異常了。 出於個人開發經驗的一點點建議: 以後連接資料庫不要這樣去連接,盡量使用Bean去完成,不然程序很混亂,以後維護非常困難。 對資料庫的操作不要顯示地去指定參數和表,尤其是參數,可以使用預處理方式,多步驟連貫操作,可以使用事務來達到數據操作的原子性,當然某些也可以通過資料庫的事務去完成。多步驟的非連貫信息的操作可以使用JDBC 3.0提供的批處理方式去完成以提高對資料庫的批量訪問成10倍得提高效率。 像在做通用模板的時候,對資料庫的表的指定的動態賦予的,當然對其某些欄位的操作也是動態的,那麼資料庫類型和參數的個數也是動態的,如果用上述過程去完成非常困難。尤其還要對一些資料庫內部的空值異常進行處理。 1、公司數據沒有改變,就改變報表的樣式。 2、公司報表總數沒有變,某些報表數據有增加或刪除的現象。 3、公司的報表有增加。

4. 在struts框架中,一個jsp頁面上的一個文本框,怎麼把輸入的數據存到資料庫中,新人求解釋

通過jsp的form表單把input text裡面的東西,通過name給submit給伺服器端,如果你是struts1,要寫ActionForm,如果是struts2,要把在Action中把你的屬性聲明好,並給出setget方法,名字都要和jsp頁面上表單的name一樣,然後如果你不用hibernate或者ibatis等框架的話,就用jdbc去獲取Connection,然後通過Connection的prepareStatement(String sql)獲取PreparedStatement,通過PreparedStatement的executeUpdate()方法把你的sql語句提交,sql語句存儲的話insert into table values();這樣

5. jsp表單提交,將內容保存資料庫!

<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>添加好友</title>

</head>
<body bgcolor="ffaaff">
<center><br><br><br><br>
<%

String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/friend";
String username = "root";
String password = "root";
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String id=request.getParameter("id");
String name=request.getParameter("name");

String tel=request.getParameter("tel");
if(id.equals("")|name.equals("")|tel.equals(""))
{
response.sendRedirect("add2.jsp");
}
else{
String sql = "insert into stu(id,name,tel) values('"+id+"','"+name+"','"+tel+"')";
stmt.executeUpdate(sql);
out.print("成功添加好友:");
name=new String(name.getBytes("ISO-8859-1"));
out.print(name);
}
stmt.close();
conn.close();

%>
<a href="admin.jsp"> 返回管理首頁</a>
</center>
</body>
</html>
用javabean做,你要提交表到給這個jsp文件就可以提交,不懂得在回問哈!

6. 如何將jsp頁面的form數據存至資料庫

通過以Struts2為結構,從Form表單輸入數據,傳入資料庫流程圖
第一步:建一個inputdata.jsp,可以輸入數據。

第二步:建一個outputdata.jsp ,可以用兩種方法獲取數據,不過在Struts2中一般用第二種,第一
種是Struts1的用法。這里的還沒進資料庫哦。要存入資料庫中,還要掉用action中相對應類的方法。見第三步。

7. 如何將JSP頁面中的表單信息保存到Mysql資料庫

獲取表單中的信息,然後插入到Mysql中
<%@pagelanguage="java"contentType="text/html;charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<%
intid=Integer.parseInt(request.getParameter("id"));
introotid=Integer.parseInt(request.getParameter("rootid"));

%>

<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gbk">
<title>Replay</title>
</head>
<body>
<formmethod="post"action="ReplayOK.jsp">
<inputtype="hidden"name="id"value="<%=id%>">
<inputtype="hidden"name="rootid"value="<%=rootid%>">
<tablealign="center">
<tr>
<td>
<inputtype="text"name="title"size="80">
</td>
</tr>

<tr>
<td>
<textareacols="80"rows="20"name="cont"></textarea>
</td>
</tr>

<tr>
<td>
<inputtype="submit"value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>

---------------------------------------------------------------
下面接收上面表單中傳過來的信息,並插入到mysql中

<%@pagelanguage="java"contentType="text/html;charset=gbk"
pageEncoding="gbk"%>
<%@pageimport="java.sql.*"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("GBK");
intid=Integer.parseInt(request.getParameter("id"));
introotid=Integer.parseInt(request.getParameter("rootid"));
Stringtitle=request.getParameter("title");
Stringcont=request.getParameter("cont").replaceAll(" ","<br/>");

Connectionconn=null;
Statementst=null;

Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=690115399");
st=conn.createStatement();

conn.setAutoCommit(false);

Stringsql="insertintoarticlevalues(null,?,?,?,?,now(),0)";
PreparedStatementpstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.setInt(2,rootid);
pstmt.setString(3,title);
pstmt.setString(4,cont);
pstmt.executeUpdate();

st.executeUpdate("updatearticlesetisleaf=1whereid="+id);

conn.commit();
conn.setAutoCommit(true);

st.close();
pstmt.close();
conn.close();
%>

<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gbk">
<title>Inserttitlehere</title>
</head>
<body>
<%response.sendRedirect("ShowArticleTree.jsp");%>
</body>
</html>
當然最好的方法還是應該用jsp+JavaBean方式。

8. JSP中怎樣把文本框的內容提交後台處理並保存到資料庫

步驟如下:

1、把文本框的內容 寫在form裡面。給他個name =「wenname」名字,寫個<input type='submit' value="提交">。

2、這樣這個文本框就會提交到後台。

3、action裡面 Sting name= request.getParameter("wenname")。

4、處理後,用jdbc連接資料庫,然後寫個SQL 加進去就好了。insert 插入,update 更新。

9. 如何把JSP數據寫到資料庫中

首先是資料庫連接代碼類:
然後在你的jsp頁面寫上調用數據連接類的增刪改查就可以了。
不懂hi我
jsp頁面中
<%
String sqlgetServiceId="select e.id from eip_service e where e.service_name_en='"+serviceName.substring(serviceName.lastIndexOf("_")+1)+"' and e.service_version=1.0";
int sid=BaseDB.queryId(sqlgetServiceId, null);
%>

BaseDB.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseDB {
public static String URL = "jdbc:oracle:thin:@192.168.174.189:1521:soadb"; //版本管理ERP資料庫配置

public static String NAME = "SVMDEV";//用戶名

public static String PWD = "SVMPWD";//密碼

public static PreparedStatement ps =null;

public static ResultSet rs =null;

public static Connection connection=null;
//獲取資料庫連接信息
public static Connection getConnection() {
try {
Class.forName("oracle.jdbc.OracleDriver");
if (connection==null) {
connection=DriverManager.getConnection(URL, NAME, PWD);
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return connection;
}

//查詢數據,根據相關信息查詢得到當前服務的某個需要的id
public static int queryId(String sql,String parameter[] ) {
int getId=0;
try {

connection=getConnection();
ps=connection.prepareStatement(sql);

if (parameter!=null) {
for (int i = 1; i <=parameter.length; i++) {
ps.setString(i,parameter[i-1]);
}
}
rs=ps.executeQuery();
if(rs.next()&&rs!=null){
getId=rs.getInt(1);
}
} catch (SQLException e) {

e.printStackTrace();
}finally{
closeAll(ps, rs, connection);
}

return getId;
}

//修改數據
public static int updateData(String sql,String parameter[] ) {
int count=0;
try {
connection=getConnection();
ps=connection.prepareStatement(sql);

if (parameter!=null) {
for (int i = 1; i <=parameter.length; i++) {
ps.setString(i,parameter[i-1]);
}
}
count=ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();
}finally{
closeAll(ps, rs, connection);
}

return count;
}
//插入數據
public static int insertData(String sql,String parameter[]) {
int num=0;
try {
connection=getConnection();
ps=connection.prepareStatement(sql);
if (parameter!=null) {
for (int i = 0; i <parameter.length; i++) {
ps.setString(i+1,parameter[i]);
}
}
num=ps.executeUpdate();
} catch (Exception e) {

e.printStackTrace();
}finally{
closeAll(ps,null,connection);
}

return num;

}
//關閉所有
public static void closeAll(PreparedStatement ps,ResultSet rs,Connection connection) {
try {
if (ps!=null) {
ps.close();
}
} catch (Exception e2) {
try {
if (rs!=null) {
rs.close();
rs=null;
}
} catch (Exception e3) {
try {
if (connection!=null) {

//connection.close();
//connection=null;
}
} catch (Exception e4) {

e4.printStackTrace();
}
}

}

}

}

熱點內容
安卓外部資源怎麼下載 發布:2024-03-29 04:01:17 瀏覽:244
華為被加密碼的相冊在哪裡查看 發布:2024-03-29 04:00:27 瀏覽:747
自動欣悅版有哪些配置 發布:2024-03-29 03:48:26 瀏覽:287
如何用腳本搶 發布:2024-03-29 03:01:59 瀏覽:120
火影忍者手游配置怎麼調 發布:2024-03-29 02:53:53 瀏覽:103
編程畫櫻花 發布:2024-03-29 02:11:24 瀏覽:473
騰訊雲伺服器1mb老掉線 發布:2024-03-29 01:56:11 瀏覽:215
執行sql語句的存儲過程 發布:2024-03-29 01:52:37 瀏覽:697
婚紗攝影腳本 發布:2024-03-29 01:47:40 瀏覽:901
我的世界伺服器咋開外掛 發布:2024-03-29 01:07:45 瀏覽:456