如何把JSP数据写到数据库中?

如何把JSP数据写到数据库中?,第1张

首先是数据库连接代码类:

然后在你的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 = 1i <=parameter.lengthi++) {

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 = 1i <=parameter.lengthi++) {

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 = 0i <parameter.lengthi++) {

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使用数据库的例子:

showByJdbcOdbc.jsp

<%@ page contentType="text/htmlcharset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML><BODY bgcolor=cyan>

<% Connection con

Statement sql

ResultSet rs

try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

}

catch(ClassNotFoundException e)

{ out.print(e)

}

try { con=DriverManager.getConnection("jdbc:odbc:mymoon","sa","sa")

sql=con.createStatement()

rs=sql.executeQuery("SELECT * FROM employee WHERE salary>3000")

out.print("<table border=2>")

out.print("<tr>")

out.print("<th width=100>"+"雇员号")

out.print("<th width=100>"+"姓名")

out.print("<th width=50>"+"出生日期")

out.print("<th width=50>"+"薪水")

out.print("</TR>")

while(rs.next())

{ out.print("<tr>")

out.print("<td >"+rs.getString(1)+"</td>")

out.print("<td >"+rs.getString(2)+"</td>")

out.print("<td >"+rs.getDate("birthday")+"</td>")

out.print("<td >"+rs.getFloat("salary")+"</td>")

out.print("</tr>")

}

out.print("</table>")

con.close()

}

catch(SQLException e)

{ out.print(e)

}

%>

</BODY></HTML>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10710100.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-10
下一篇 2023-05-10

发表评论

登录后才能评论

评论列表(0条)

保存