如何把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()

}

}

}

}

}

Class.forName("com.mysql.jdbc.Driver")

Connection

con=

DriverManager.getConnection("jdbc:mysql://10.10.2.188:3306/bbs?useUnicode=true&characterEncoding=utf8",

"root","yaoweijq")

Statement

stat=

con.createStatement()

第一句是加载数据库驱动,

自己下一个加到类库中,

我不知道要不要放到tomcat目录下。

第二句是连接数据库,

IP自己改,测试用localhost

3306是端口,mysql的

后面是数据库名你也可以用表名

再后面root是用户名yaoweijq是密码

都是mysql的

大体就是这样

本例设计到三个类和两个页面User类(数据实体),UserManager(封装了Dao),DBUtil(封装了数据库常用 *** 作)index页面(添加用户页面),add页面(连库持久化页面),具体解释我都写在注释里了,请参考

User类

package com.sjzcvc.recover

public class User {

private int id

private String username

private String password

public int getId() {

return id

}

public void setId(int id) {

this.id = id

}

public String getUsername() {

return username

}

public void setUsername(String username) {

this.username = username

}

public String getPassword() {

return password

}

public void setPassword(String password) {

this.password = password

}

}

DBUtil类

package com.sjzcvc.recover

import java.sql.*

public class DBUtil {

private final String DRIVER = "oracle.jdbc.driver.OracleDriver"

private final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"

private final String USERNAME = "scott"

private final String PASSWORD = "tiger"

private Connection conn = null

public Connection getConnection(){

try {

//注册驱动

Class.forName(this.DRIVER)

//获取连接

conn = DriverManager.getConnection(this.URL,this.USERNAME,this.PASSWORD)

} catch (ClassNotFoundException e) {

e.printStackTrace()

} catch (SQLException e) {

e.printStackTrace()

}

return conn

}

public static void main(String args[]){

DBUtil dbUtil = new DBUtil()

Connection conn = dbUtil.getConnection()

}

}

UserManager类

package com.sjzcvc.recover

import java.sql.*

public class UserManager {

public void addUser(User user) {

DBUtil dbUtil = new DBUtil()

Connection conn = null

String sql = "insert into t_user values(?,?,?)"

PreparedStatement pstmt = null

try {

conn = dbUtil.getConnection()

// 准备好要执行sql语句的pstmt

pstmt = conn.prepareStatement(sql)

pstmt.setInt(1, user.getId())

pstmt.setString(2, user.getUsername())

pstmt.setString(3, user.getPassword())

// 执行插入 *** 作

pstmt.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

} finally {

// 关闭连接释放资源

try {

pstmt.close()

conn.close()

} catch (SQLException e) {

e.printStackTrace()

}

}

}

public User queryUser(String username) {

DBUtil dbUtil = new DBUtil()

Connection conn = null

String sql = "select * from t_user where username=?"

PreparedStatement pstmt = null

ResultSet rs = null

User user = null

try {

conn = dbUtil.getConnection()

// 准备好要执行sql语句的pstmt

pstmt = conn.prepareStatement(sql)

pstmt.setString(1, username)

// 执行查询 *** 作

rs = pstmt.executeQuery()

if(rs.next()){

String password = rs.getString("password")

user = new User()

user.setId(8)

user.setUsername(username)

user.setPassword(password)

}

System.out.println("add user success!!!")

} catch (SQLException e) {

e.printStackTrace()

} finally {

// 关闭连接释放资源

try {

pstmt.close()

conn.close()

} catch (SQLException e) {

e.printStackTrace()

}

}

return user

}

public static void main(String args[]) {

UserManager um = new UserManager()

// User user = new User()

// user.setId(1)

// user.setUsername("张三")

// user.setPassword("999999")

//

// um.addUser(user)

User user = um.queryUser("masanli")

System.out.println(user.getPassword())

}

}

index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<html>

<body>

User regist

<form action="add.jsp" method="post">

Username:<input type="text" name="username"/><br>

Password:<input type="password" name="password"/><br>

<input type="submit" value="submit"/>

</form>

</body>

</html>

add.jsp页面

<%@ page language="java" contentType="text/htmlcharset=GB18030"

pageEncoding="GB18030"%>

<%@ page import="com.sjzcvc.recover.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=GB18030">

<title>Insert title here</title>

</head>

<body>

<%

//获取表单中提交过来的两个参数username和pasword

String username = request.getParameter("username")

String password = request.getParameter("password")

//把两个参数封装到user对象中

User user = new User()

user.setId(2)

user.setUsername(username)

user.setPassword(password)

//调用添加用户的方法

UserManager um = new UserManager()

um.addUser(user)

out.print("add success!!!")

%>

</body>

</html>


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存