首先要写个DBManager类(名字任取,知道数数据库 *** 作类就可以了),然后写Action类
DBManager:
package com.test
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
public class DBManager {
Connection con = null// 数据库连接
PreparedStatement pre = null// 预编译语句对象
ResultSet result = null// 结果集
//连接数据库
public void conOracle() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()// 加载oracle驱动
System.out.println("开始尝试数据库连接!")
String url = "jdbc:oracle:thin:@10.a.b.c:1521:SXXXXX"
String user = "vika" //数据库连接名
String password = "123" //连接密码
con = DriverManager.getConnection(url, user, password)// 获取连接
System.out.println("连接成功!")
} catch (Exception e) {
e.printStackTrace()
System.out.println("")
}
}
//获取编译器方法
public PreparedStatement getPre(String sql) throws SQLException {
return con.prepareStatement(sql)
}
//关闭数据库方法:
public void closeConnection() {
try {
if (result != null)
result.close()
if (pre != null)
pre.close()
if (con != null)
con.close()
System.out.println("数据库连接已关闭")
} catch (Exception e) {
e.printStackTrace()
}
}
}
Action类:
package com.test
import java.sql.PreparedStatement
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.apache.struts.action.Action
import org.apache.struts.action.ActionForm
import org.apache.struts.action.ActionForward
import org.apache.struts.action.ActionMapping
public class TestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 先把url中的数据取出来
String reId = request.getParameter("reid")
String reCard = request.getParameter("recard")
String reeName = request.getParameter("reename")
String rwId = request.getParameter("rwid")
String rwName = request.getParameter("rwname")
String rdepId = request.getParameter("rdepid")
String rdepName = request.getParameter("rdepname")
String rResult = request.getParameter("rresult")
String rbId = request.getParameter("rbid")
String rticketTime = request.getParameter("rtickettime")
String rstartTime = request.getParameter("rstarttime")
String rTime = request.getParameter("rtime")
String rtakeTime = request.getParameter("rtaketime")
String rcusInfo = request.getParameter("rcusinfo")
String rqueueId = request.getParameter("rqueueid")
// 数据库中表建好了吧?先连接数据库(调用DBManager *** 作类的方法),
// 对应列插入,要对应起来,不然会引起类型不匹配等错误
DBManager db = new DBManager()
boolean f = false//标识插入是否成功,成功返回true否则为false
try {
db.conOracle()
String sqlStr = "insert into EXXXXX values('" + reId + "','"
+ reCard + "','" + reeName + "','" + rwId + "','" + rwName
+ "','" + rdepId + "','" + rdepName + "','" + rResult
+ "','" + rbId + "','" + rticketTime + "','" + rstartTime
+ "','" + rTime + "','" + rtakeTime + "','" + rcusInfo
+ "','" + rqueueId + "')"
PreparedStatement pre = db.getPre(sqlStr) //sql语句装入编译器
pre.execute(sqlStr) //执行该语句
f = true//成功则继续执行,f=true;
} catch (Exception e) {
e.printStackTrace() //不成功,跳入异常,并f=false标识结果
f = false
}
finally{
db.closeConnection()
}
ActionForward af = null
if (f == true) { //成功的话,跳去struts配置中的success页面
af = mapping.findForward("success")
} else {
af = mapping.findForward("error")//失败跳error页面
}
return af
}
}
在Struts-config.xml中配置action
在action-Mappings标签下添加:
<action path="/addTranstits" type="com.test.TestAction "(包名+类名)>
//成功时的跳转页面,需先写好,否则无法跳转报错 ,对应Action中f=true时findforward的表单
<forward name="success" path="/page1.jsp"/>
//失败时跳转页面,也可以没有,对应Action中的分false的情况跳转
<forward name="error" path="/page2.jsp"/>
</action>
点击之后处理?
添加方法即可啊
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.js"></script>
<title></title>
</head>
<body>
<form id="submitForm" action="#">
<input value="123" type="text" class="val" id="val1" /><br />
<input value="234" type="text" class="val" id="val2" /><br />
<input value="345" type="text" class="val" id="val3" /><br />
<input value="456" type="text" class="val" id="val4" /><br />
</form>
<button onclick="submitMethod()">提交</button>
</body>
<script>
function submitMethod(){
//封装数组
$(".val").val("1")
//提交表单
$("#submitForm").submit()
}
</script>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)