本文目錄一覽:
如何把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();
}
}
}
}
}
用jsp向數據庫插入數據
你的問題我知道了,你想往數據庫里插入數據,單純從jsp頁面插入沒有現實意義,可以考慮到再編寫一個表單頁面提交表單數據,在jsp頁面用統配符向數據庫插入數據。
我大致一個小例子你看看。
zhuce.html
html
body
form name=”form1″ method=”post” action=”register.jsp”
p align=”center”用戶名:
input type=”text” name=”name”
/p
p align=”center”密碼:
input type=”password” name=”password”
/p
p align=”center”
input type=”submit” name=”Submit” value=” 注 冊”
/p
/form
/body
/html
register.jsp
%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”” %
html
body
%
request.setCharacterEncoding(“GBK”);
String name=request.getParameter(“name”);//內置對象應該會吧
String password=request.getParameter(“password”);
try{
Class.forName(“org.gjt.mm.mysql.Driver”); //驅動程序你自己的,我的是com.mysql.jdbc.Driver
String url=”jdbc:mysql://localhost:3306/tian”;//你自己設置數據庫名稱
Connection con=DriverManager.getConnection(url,”root”,””); //如果你mysql中root的密碼是空的話最好寫成””代替null
String sql=”insert into txt (name,password) values (‘”+name+”‘,'”+password+”‘)”;//你使用的表是txt,sql建表自己看着辦吧
Statement stmt=con.createStatement();
if{
stmt.executeUpdate(sql);
response.sendRedirect(“success.html”);//根據結果定向成功頁面
}else{
response.sendRedirect(“f.html”);//失敗頁面
}
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
%
/body
/html
至於success.jsp和f.jsp比較簡單自己寫下吧。
不會了可以上網查資料,或許再提問吧
jsp選項該怎麼存數據庫
首先在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、公司的報表有增加。 尤其是後面的兩者,如果沒有通用模板,當發生這些情況,就需要重新編碼而且對程序進行從新編譯的過程)
JSP里怎麼往數據庫寫入數據?
你的意思不是很明確啊!
比如你的jsp頁面中又2個表單元素input type=text name=”name”
input type=password name=”password”
你填完表單後提交到Servlet還是到一個javaBean啊?
比如你到一個Servlet中用
String a=request.getParameter(“name”);//能取得你表單元素的值
然後用insert into 表名 values(a);這樣就可以把你添加到表單中的值
插入到數據庫中!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/270475.html