jsp javabean 读取数据库
上次编了一个程序:用程序建表。然后用一界面录入数据。(代码贴出来了)现在想改成用javabean完成这一功能。。。但是不知道怎么改大家帮帮忙谢谢啦~------5-1.j...
上次编了一个程序:用程序建表。然后用一界面录入数据。(代码贴出来了)
现在想改成用javabean完成这一功能。。。 但是不知道怎么改 大家帮帮忙 谢谢啦~
------5-1.jsp----------
<%@page contentType="text/html;charset=GB2312"%>
<%@page language="java"%>
<!DOCTYPE HTML PUBLIC"-//w2c//dtd html 4.0 transitional//en">
<body><a href="5-2.jsp">建表</a>
<form action="5-3.jsp" method="post" name="form">
<label>顾客信息:</label><br>
姓名:<input name="parm1" value=""></input><br>
电话:<input name="parm2" value=""></input><br>
地址:<input name="parm3" value=""></input><br>
<input type="submit" name="Submit2" value="提交">
<input type="reset" name="Submit" value="清空">
</form>
</body>
-------5-2.jsp---------------------------------
<%@page contentType="text/html;charset=GB2312"%>
<%
request.setCharacterEncoding("GBK");
java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.PreparedStatement preparedStmt=null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("create table biao(name char(30),phone char(30),address char(30));");
preparedStmt.executeUpdate();
out.println("完成建表");
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch(java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{if(conn!=null)conn.close();}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
}
}
%>
------5-3.jsp---------------------------------
<%@page contentType="text/html;charset=GB2312"%>
<%
java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.PreparedStatement preparedStmt=null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("insert into biao(name,phone,address)"+"value(?,?,?)");
String name=request.getParameter("parm1");
String phone=request.getParameter("parm2");
String address=request.getParameter("parm3");
preparedStmt.setString(1,name);
preparedStmt.setString(2,phone);
preparedStmt.setString(3,address);
preparedStmt.executeUpdate();
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch(java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{
if(conn!=null)conn.close();
}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
}
}
%> 展开
现在想改成用javabean完成这一功能。。。 但是不知道怎么改 大家帮帮忙 谢谢啦~
------5-1.jsp----------
<%@page contentType="text/html;charset=GB2312"%>
<%@page language="java"%>
<!DOCTYPE HTML PUBLIC"-//w2c//dtd html 4.0 transitional//en">
<body><a href="5-2.jsp">建表</a>
<form action="5-3.jsp" method="post" name="form">
<label>顾客信息:</label><br>
姓名:<input name="parm1" value=""></input><br>
电话:<input name="parm2" value=""></input><br>
地址:<input name="parm3" value=""></input><br>
<input type="submit" name="Submit2" value="提交">
<input type="reset" name="Submit" value="清空">
</form>
</body>
-------5-2.jsp---------------------------------
<%@page contentType="text/html;charset=GB2312"%>
<%
request.setCharacterEncoding("GBK");
java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.PreparedStatement preparedStmt=null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("create table biao(name char(30),phone char(30),address char(30));");
preparedStmt.executeUpdate();
out.println("完成建表");
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch(java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{if(conn!=null)conn.close();}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
}
}
%>
------5-3.jsp---------------------------------
<%@page contentType="text/html;charset=GB2312"%>
<%
java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.PreparedStatement preparedStmt=null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("insert into biao(name,phone,address)"+"value(?,?,?)");
String name=request.getParameter("parm1");
String phone=request.getParameter("parm2");
String address=request.getParameter("parm3");
preparedStmt.setString(1,name);
preparedStmt.setString(2,phone);
preparedStmt.setString(3,address);
preparedStmt.executeUpdate();
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch(java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{
if(conn!=null)conn.close();
}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
}
}
%> 展开
展开全部
你就把所有的业务代码写在一个Java类里面就OK啦!
MyBean.java
package cn.imust;
import java.sql.*;
public class MyBean{
public void createTable(){
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("create table biao(name char(30),phone char(30),address char(30));");
preparedStmt.executeUpdate();
out.println("完成建表");
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{if(conn!=null)conn.close();}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
public void insertDate(){
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("insert into biao(name,phone,address)"+"value(?,?,?)");
String name=request.getParameter("parm1");
String phone=request.getParameter("parm2");
String address=request.getParameter("parm3");
preparedStmt.setString(1,name);
preparedStmt.setString(2,phone);
preparedStmt.setString(3,address);
preparedStmt.executeUpdate();
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch(java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{
if(conn!=null)conn.close();
}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
}
}
}
}
然后在JSP里面定义这个bean,想创建表的时候 只需要调用这个bean的createTable()就OK啦!!
代码有点乱 都是你自己写的 呵呵 这样的代码 有太多地方可以优化的哦
抓紧时间练吧!
MyBean.java
package cn.imust;
import java.sql.*;
public class MyBean{
public void createTable(){
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("create table biao(name char(30),phone char(30),address char(30));");
preparedStmt.executeUpdate();
out.println("完成建表");
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{if(conn!=null)conn.close();}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
public void insertDate(){
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
preparedStmt=conn.prepareStatement("insert into biao(name,phone,address)"+"value(?,?,?)");
String name=request.getParameter("parm1");
String phone=request.getParameter("parm2");
String address=request.getParameter("parm3");
preparedStmt.setString(1,name);
preparedStmt.setString(2,phone);
preparedStmt.setString(3,address);
preparedStmt.executeUpdate();
}catch(java.sql.SQLException e){
out.println(e.toString());
}finally{
if(preparedStmt!=null)try{preparedStmt.close();}catch(java.sql.SQLException e1){
out.println(e1.toString());
}finally{
try{
if(conn!=null)conn.close();
}catch(java.sql.SQLException e2){
out.println(e2.toString());
}
}
}
}
}
然后在JSP里面定义这个bean,想创建表的时候 只需要调用这个bean的createTable()就OK啦!!
代码有点乱 都是你自己写的 呵呵 这样的代码 有太多地方可以优化的哦
抓紧时间练吧!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询