jsp javabean mysql_Jsp,JavaBean,Mysql的连接方法

利用JSP实现Web与数据库的连接:

(1).完成环境设置,导入java.sql包,命令如下:

import java.sql.*

(2).载入驱动器

(3).连接到数据库

(4).语句接口

(5).获得结果集

1. 测试环境:

windows 2003

j2sdk1.4.2_06

Tomcat 5.0

mysql-4.0.23-win

mm.mysql-2.0.4-bin.jar

2. 建立数据库及表

在Mysql中建个publish数据库,并建个book表。在book中添加id,title,price

3. JavaBean的代码:  dbconn.java

package Border;

import java.sql.*; //完成环境设置,导入java.sql包

public class dbconn {

public dbconn()

{

}

//declare variable

private Connection conn = null;

private ResultSet rs = null

private String server = "127.0.0.1";   // Can''''t use localhost , you must use IP or CNAME

private String port = "3306";  //change to your port

private String db = "publish";  //change to your db name

private String user = "root"; //change to your username

private String pass = "root"; //change to your password

private String drivername="org.gjt.mm.mysql.Driver"; //mysql driver

private String URL="jdbc:mysql://"+server+":"+port+"/"+db+"?useUnicode=true&characterEncoding=GBK&user="+user+"&password="+pass;

public Connection getConn(){  //get database connection

try{

Class.forName(drivername).newInstance(); //载入驱动器

conn = DriverManager.getConnection(URL); //连接到数据库

}

catch(Exception e){

e.printStackTrace();

}

return conn ;

}

public ResultSet executeSQL(String str) {

try{

Statement stmt = conn.createStatement(); //语句接口

rs = stmt.executeQuery(str);  //获得结果集

}

catch(Exception e){

e.printStackTrace();

}

return rs;

}

}

编译javac dbconn.java,将编译后的文件dbconn.class放到目录 "你的项目"/WEB-INF/classes/Border/下

4. 调用Java Bean的JSP文件test.jsp

ResultSet rs = null;

Connection conn = null;

conn = Border.getConn() ;

rs = Border.executeSQL("select * from book");

%>

My first Jsp  JavaBean  Mysql

id

title

price

while(rs.next()) {

%>

rs.close();

conn.close();

%>

title:
price:

5. update.jsp 写入数据库

Connection conn = null;

conn = Border.getConn() ;

String tit=request.getParameter("Title");

String prc=request.getParameter("Price");

Statement stmt = conn.createStatement();

stmt.executeUpdate("INSERT INTO book (title,price) VALUES (''''"+tit+"'''',''''"+prc+"'''')");

%>

6. 提供几个函数:

(1). getConnection

public static Connection getConnection(String url,

String user,

String password)

throws SQLException

Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:

url - a database url of the form jdbc:subprotocol:subname

user - the database user on whose behalf the connection is being made

password - the user''''s password

Returns:

a connection to the URL

Throws:

SQLException - if a database access error occurs

(2). createStatement

public Statement createStatement()

throws SQLException

Creates a Statement object for sending SQL statements to the database. SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.

Result sets created using the returned Statement object will by default be type TYPE_FORWARD_ONLY and have a concurrency level of CONCUR_READ_ONLY.

Returns:

a new default Statement object

Throws:

SQLException - if a database access error occurs

(3). executeQuery

public ResultSet executeQuery(String sql)

throws SQLException

Executes the given SQL statement, which returns a single ResultSet object.

Parameters:

sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement

Returns:

a ResultSet object that contains the data produced by the given query; never null

Throws:

SQLException - if a database access error occurs or the given SQL statement produces anything other than a single ResultSet object

(4). getString

public String getString(String columnName)

throws SQLException

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Parameters:

columnName - the SQL name of the column

Returns:

the column value; if the value is SQL NULL, the value returned is null

Throws:

SQLException - if a database access error occurs

(5). next

public boolean next()

throws SQLException

Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object''''s warning chain is cleared when a new row is read.

Returns:

true if the new current row is valid; false if there are no more rows

Throws:

SQLException - if a database access error occurs

7. ok! Now game is over~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于jsp+javabean+mysql三层结构的动态购物网站。网站用户接口(即界面)由jsp完成,数据和逻辑处理由beans完成,数据储存由mysql完成。因为beans独立负责处理整个网站的全部数据逻辑运算,所以整个网站的负载量和速度都将大大提高,所以基于这种语言和结构开发的购物系统的优势是其它语言没法比尔的。更重要的是,jsp+bean能够在Apache环境下顺畅地运行,这也是其最大优点之一。这就更进一步保证了网站的稳定性和安全性,而这些,对于一个购物网站来说是非常重要的!这个版本让大家期待已久了,主要做了以下一些修正:1.新增产品站内搜索引擎。2.新增人气商品排行。3.新增热评商品排行。4.新增重点推荐产品。5.删除不太需要的新闻频道。6.修正后台管理的一些Nullpointexception错误。7.新增顾客资料修改。8.新增顾客订单查询。9.新增后台超级用户和订单管理员管理,方便物流人员独立处理订单。10.完善了一些购物流程。本程序由“城市WebClub”独立自主开发,你可以对整个商城的版面甚至版权作任意修改,但传播本程序时,请保留本站的说明文件。因为开发一个系统真的很痛苦!由于时间和水平关系,本系统还有很多不足和错误的地方,欢迎大家试用并提出意见。安装====1。把myshop直接上传到你的jsp主机。2。把class里的文件上传到你的jsp主机的class目录。3。分别修改myshop下的config.jsp、class/myshop下的config.java文件。4。把所有mysqldata文件倒入你的数据库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值