📄 0194.htm
字号:
<html>
<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>又一个jsp+bean操作数据库的例子</strong></big></p>
<div align="right">---摘自互联网</div>
<br>JSP<br>
<%@page import="hello.*;" errorPage="DisplayError.jsp"%><br>
<jsp:useBean id="DbBean" class="hello.DeveloperBean"/><br>
<html><br>
<head><br>
<h1>New User Registration</h1><br>
<body bgcolor="grey" text="blue" ><br>
<p><br>
<br><br>
<form name="registration" action="CreateUser.jsp"><br>
<pre><br>
LoginId <input type =text name="loginid"><br>
Password <input type =password name="password"><br>
First Name <input type =text name="firstname"><br>
last name <input type =text name="lastname"><br>
Company <input type =text name="companyname"><br>
StreetAddr <input type =text name="streetaddress"><br>
City <input type =text name="city"><br>
State <input type =text name="state"><br>
Zip <input type =text name="zip"><br>
Phone <input type =text name="phone"><br>
</pre><br>
<Input type = submit name="create" value="Create" ><br>
<Input type=Reset><br>
<input type=button value="Back To Home Page" onClick="window.history.back()"><br>
</form><br>
<%<br>
if ( request.getParameter("create")!=null)<br>
{<br>
String btnvalue = request.getParameter("create");<br>
String loginid = request.getParameter("loginid");<br>
if ( btnvalue.equals("Create"))<br>
{<br>
if ( DbBean.exists(loginid))<br>
{<br>
out.println("User Already Exists");<br>
}<br>
else<br>
{<br>
%><br>
<jsp:setProperty name="DbBean" property="password"/><br>
<jsp:setProperty name="DbBean" property="firstname"/><br>
<jsp:setProperty name="DbBean" property="lastname"/><br>
<jsp:setProperty name="DbBean" property="companyname"/><br>
<jsp:setProperty name="DbBean" property="streetaddress"/><br>
<jsp:setProperty name="DbBean" property="city"/><br>
<jsp:setProperty name="DbBean" property="state"/><br>
<jsp:setProperty name="DbBean" property="zip"/><br>
<jsp:setProperty name="DbBean" property="phone"/><br>
<%<br>
if (DbBean.setLoginId(loginid))<br>
{<br>
out.println("User "+loginid+" created");<br>
}<br>
}<br>
}<br>
}<br>
%><br>
</body><br>
</head><br>
</html><br>
<br>
Bean<br>
<br>
package databean;<br>
<br>
import java.sql.*;<br>
<br>
public class DbBean {<br>
static Connection con;<br>
static String dsnName ="jdbc:odbc:yourdsnname" ;<br>
<br>
public DbBean() throws ClassNotFoundException,SQLException {<br>
}<br>
public static Connection getDBConnection() {<br>
try {<br>
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br>
con = DriverManager.getConnection(dsnName);<br>
}catch(ClassNotFoundException e){<br>
}catch(SQLException e){<br>
}<br>
return con;<br>
}<br>
<br>
public String[] getResultSet(String query) throws SQLException {<br>
ResultSet rs1 = null,rs2=null;<br>
int i=0,counter=0;<br>
String[] name = null;<br>
Statement stmt1=null,stmt2=null;<br>
try {<br>
stmt1 = DbBean.getDBConnection().createStatement();<br>
rs2 = stmt1.executeQuery(query);<br>
while(rs2.next()){<br>
counter++;<br>
}<br>
<br>
<br>
stmt2 = DbBean.getDBConnection().createStatement();<br>
rs1 = stmt2.executeQuery(query);<br>
name = new String[counter];<br>
while(rs1.next()) {<br>
name = new String();<br>
name[i] = rs1.getString(1);<br>
i++;<br>
}<br>
<br>
<br>
}<br>
catch(SQLException e){<br>
e.printStackTrace() ;<br>
}<br>
return name;<br>
}<br>
<br>
public int getUpdataInsertDeleteCount(String query) throws SQLException {<br>
int count=0;<br>
Statement stmt=null;<br>
try {<br>
stmt = DbBean.getDBConnection().createStatement();<br>
count = stmt.executeUpdate(query);<br>
<br>
<br>
}<br>
catch(SQLException e){}<br>
return count;<br>
}<br>
}<br>
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -