JavaBean与jsp实现对数据库的增加、修改和删除的操作的实例



以下为大家介绍关于JavaBean与jsp实现对数据库的增加、修改和删除的操作的源码教程,包含了处理数据库的java文件源码,以及两个jsp文件源码,具体情况如下:

JDBConnection.java 文件也就是对数据库的相关操作增删查改的java源代码文件:

package com;
import java.sql.*;

public class JDBConnection {
private final String dbDriver = “com.microsoft.jdbc.sqlserver.SQLServerDriver”; //连接sql数据库的方法
private final String url = “jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database02″;
private final String userName = “sa”;
private final String password = “”;
private Connection con = null;

public JDBConnection() {
try {
Class.forName(dbDriver).newInstance(); //加载数据库驱动
} catch (Exception ex) {
System.out.println(“数据库加载失败”);
}
}

//创建数据库连接
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);

} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println(“creatConnectionError!”);
}
return true;
}

//对数据库的增加、修改和删除的操作
public boolean executeUpdate(String sql) {

if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println(“操作成功,所影响的记录数为” + String.valueOf(iCount));
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println(“executeUpdaterError!”);
}
return true;
}

//对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println(“executeQueryError!”);
return null;
}
return rs;
}

//关闭数据库的操作
public void closeConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.out.println(“Failed to close connection!”);
} finally {
con = null;
}
}
}


}

相关的jsp源码文件1:

<%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”" %>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>修改数据时下拉列表的默认值为数据库中原数据信息</title>
<jsp:useBean id=”connection” scope=”request” class=”com.JDBConnection”/>
<link href=”Css/Css.css” type=”text/css” rel=”stylesheet”>
</head>

<body>
<table width=”500″ height=”259″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″ background=”images/0.jpg”>
<tr align=”center”>
<td height=”82″ colspan=”2″><strong><font color=”#FFFFFF” size=”+2″>修改用户信息模块</font></strong></td>
</tr>
<tr>
<td width=”87″>&nbsp;</td>
<td width=”413″ valign=”top”><table width=”382″ border=”0″ align=”center”>
<tr align=”center”>
<td width=”66″>账号</td>
<td width=”66″>性别</td>
<td width=”66″>年龄</td>
<td width=”66″>职业</td>
<td width=”84″>操作</td>
</tr>
<%
String sql=”select * from tb_userInfo”;
ResultSet rs=connection.executeQuery(sql);
try{
while(rs.next()){
String id=rs.getString(1);
%>
<tr align=”center”>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><a href=”dealwith.jsp?id=<%=id%>”>修改</a></td>
</tr>
<%}}catch (Exception e){}%>
</table></td>
</tr>
</table>
</body>
</html>

相关的jsp源码文件2:

<%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”" %>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<link href=”Css/Css.css” type=”text/css” rel=”stylesheet”>
<jsp:useBean id=”connection” scope=”request” class=”com.JDBConnection”/>

<title>修改用户信息页面</title>
</head>
<%
String sql=”select * from tb_userInfo where id=’”+request.getParameter(“id”)+”‘”;
String profession=”";
ResultSet rs=connection.executeQuery(sql);
try{
while (rs.next()){
profession=rs.getString(“profession”);
}
}catch (Exception e){}
String pro[]={“Java程序员”,”JSP程序员”,”VC程序员”,”VF程序员”,”VB程序员”};
%>
<body>
<table width=”500″ height=”259″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″ background=”images/0.jpg”>
<tr align=”center”>
<td height=”82″ colspan=”2″><strong><font color=”#FFFFFF” size=”+2″>修改用户信息页面</font></strong></td>
</tr>
<tr>
<td width=”87″>&nbsp;</td>
<td width=”413″ valign=”top” align=”center”><table width=”289″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<form name=”form1″ method=”post” action=”">
<tr align=”center”>
<td width=”88″ height=”30″>账号</td>
<td width=”201″><div align=”left”>
<input type=”text” name=”account”>
</div></td>
</tr>
<tr align=”center”>
<td height=”30″>性别</td>
<td><div align=”left”>
<input type=”text” name=”sex”>
</div></td>
</tr>
<tr align=”center”>
<td height=”34″>年龄</td>
<td><div align=”left”>
<input type=”text” name=”age”>
</div></td>
</tr >
<tr align=”center”>
<td height=”30″>职业</td>
<td>
<div align=”left”>
<select name=”profession”>
<%
for(int i=0;i<pro.length;i++){
%>

<option value=”<%=pro[i]%>” <%if(pro[i].equals(profession)){%>selected <%}%>><%=pro[i]%></option>
<%}%>
</select>

</div></td>
</tr >
<tr align=”center”>
<td height=”30″>&nbsp;</td>
<td>
<input type=”submit” name=”Submit2″ value=”修改”>&nbsp;
<input type=”reset” name=”Submit” value=”重置”>&nbsp;
<input type=”button” name=”Submit3″ value=”返回” onClick=”javascript:window.location.href=’index.jsp’;”>
</td>
</tr></form>
</table></td>
</tr>
</table>
</body>
</html>

以上jsp文件你可以自己命名,然后部署到你的javaweb项目中去,我采用的是myeclipse与tomcat开发的,数据库采用的是SQLServer。