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
<%@ page contentType="text/html;charset=GBK" import="java.sql.*"%>
<jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%
ResultSet rs = null;
Connection conn = null;
conn = Border.getConn() ;
rs = Border.executeSQL("select * from book");
%>
<html>
<body>
<br>
<h2 align="center" > My first Jsp JavaBean Mysql </h2>
<br>
<table border="1" align="center">
<tr>
<th>
id
</th>
<th>
title
</th>
<th>
price
</th>
</tr>
<%
while(rs.next()) {
%>
<tr>
<th>
<%=rs.getString("id")%>
</th>
<th>
<%=rs.getString("title")%>
</th>
<th>
<%=rs.getString("price")%>
</th>
</tr>
<%}%>
<%
rs.close();
conn.close();
%>
</table>
<form name="form1" method="post" action="update.jsp">
<table width="210" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="77">title:</td>
<td width="127"><label>
<input name="Title" type="text" id="Title">
</label></td>
</tr>
<tr>
<td>price:</td>
<td><label>
<input name="Price" type="text" id="Price">
</label></td>
</tr>
<tr>
<td><div align="right">
<label>
<input type="submit" name="Submit" value="提交">
</label>
</div></td>
<td><label>
<input type="reset" name="Submit2" value="重置">
</label></td>
</tr>
</table>
</form>
</body>
</html>
5. update.jsp 写入数据库
<%@ page contentType="text/html;charset=GBK" import="java.sql.*"%>
<% request.setCharacterEncoding("GBK"); %>
<jsp:useBean id="Border" scope="page" class="Border.text" />
<%
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+"')");
%>
<jsp:forward page="text.jsp" />
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~~
8. 若您觉得满意,想转载或收藏这篇文章,我非常感谢,但请您注明作者: border ( border@hacker.cn )
http://blog.csdn.net/border1985
最后欢迎大家访问 --中国安全信息网-- http://www.hacker.cn/
分享到:
相关推荐
总结来说,`jsp连接mysql数据库的javabean` 是一种通过JavaBean实现的数据库连接组件,它简化了JSP与MySQL之间的交互,使得数据访问更加方便且易于管理。`DBConnect.java` 文件中的关键组件包括数据库连接信息、连接...
【标题】"jsp.rar_java jsp_javaBean mysql_javabean_jsp mysql_mysql jsp" 涉及到的关键技术主要包括Java、JSP、JavaBean、以及MySQL数据库,这些都是Web开发中的重要组成部分。 【Java】:Java是一种广泛使用的...
JavaBean遵循一定的规范,如公开默认构造器、属性和getter/setter方法,使得它们可以被方便地在JSP中使用,或者通过其他Java代码进行操作。 **MySQL数据库** MySQL是一种关系型数据库管理系统,被广泛用于存储和...
【标题】:“jsp+javaBean+Mysql实现BBS系统”是基于Web开发的一个经典案例,它涵盖了网页交互、业务逻辑处理以及数据存储的核心技术。在这个系统中,JSP(JavaServer Pages)作为视图层负责展示用户界面,JavaBean...
【JSP+JAVABEAN+MYSQL网络硬盘】是一个典型的Web应用开发案例,适合初学者理解Web开发的基本流程和核心技术。在这个项目中,JSP(JavaServer Pages)用于前端展示,JAVABEAN作为业务逻辑处理,而MySQL则是后台数据库...
"jsp_mysql_tomcat"则强调了JSP、MySQL和Tomcat之间的集成,JSP页面通过Servlet或者JDBC连接到MySQL数据库,Tomcat作为服务器支持整个应用程序的运行。"oa_mysql"标签明确了这是关于办公自动化系统的,且使用MySQL...
本项目采用经典的Web开发技术栈,即`JSP+JavaBean+MySQL`,构建了一个高效、稳定且易于维护的新闻发布系统。这个架构在Web开发领域具有广泛的应用,尤其适合中小型企业的信息管理系统。 1. **JSP(JavaServer Pages...
《JSP+MYSQL选课系统详解》 JSP(JavaServer Pages)与MYSQL结合构建的选课系统,是教育信息化领域常见的技术应用。这个系统基于Web,能够在Tomcat服务器上运行,为学生、教师和管理员提供便捷的在线选课、管理功能...
【jsp+JavaBean+Mysql简易留言板】是一个基础的Web应用程序示例,主要展示了如何使用JSP(JavaServer Pages)技术、JavaBean组件以及MySQL数据库来实现一个简单的用户交互功能,即留言功能。这个项目对于初学者来说...
【标题】"wangshangshudian.rar" 提供了一个基于JavaBean、MySQL数据库和JSP技术实现的网上书店系统。这个项目展示了如何利用这些技术来构建一个完整的Web应用程序,让用户能够在线浏览书籍、搜索图书并进行购买操作...
**JSP+Servlet+Javabean+Mysql学生成绩管理系统详解** 在现代Web开发中,构建一个高效、易维护的学生成绩管理系统是教育信息化的重要组成部分。本系统采用经典的MVC(Model-View-Controller)架构模式,结合了JSP、...
标题 "java网上商城项目源码(jsp.servlet+javabean+mysql+jdbc)" 提供了一个基于Java技术的网上商城项目的完整源代码实现。这个项目利用了JSP(Java Server Pages)、Servlet、JavaBean以及MySQL数据库和JDBC(Java...
4. **连接数据库**:在JSP或JavaBean中配置数据库连接,使用JDBC(Java Database Connectivity)接口与数据库交互。 5. **实现功能**:实现注册、登录、查询、添加、修改和删除学生信息等功能。 6. **测试**:最后,...
在本示例中,JSP可能包含用于显示数据的JSP标签,以及调用Servlet或JavaBean的方法来处理请求。 **Servlet** 是Java Web应用中的服务器端组件,负责接收和响应HTTP请求。在JSP+Servlet+JavaBean架构中,Servlet常常...
【标题】"jsp+JavaBean+Servlet+MySQL宿舍管理系统(初学者)"是一个针对初学者设计的项目,旨在帮助他们理解并实践Web应用开发的基本技术。这个系统的核心是使用JSP(JavaServer Pages)来展示界面,JavaBean作为业务...
【标题】"购物系统(jsp+JavaBean+mysql)"是一个基于Web的电子商务应用程序,它利用了JavaServer Pages(JSP)、JavaBeans以及MySQL数据库来实现一个基础的在线购物平台。这个项目旨在帮助初学者理解如何将前端界面...
《基于JSP、JavaBean、MySQL的网上书店系统详解》 网上书店系统是现代电子商务的重要组成部分,它将传统的图书销售模式与互联网技术相结合,为用户提供便捷的购书体验。本系统采用的技术栈主要包括JSP(JavaServer ...
JavaBean可以封装数据库连接、查询方法等逻辑,使得页面更加清晰。以下是一个简单的JavaBean示例: ```java public class PagingBean { private Connection conn; private int pageSize; private int current...
通过属性和方法,Javabean可以方便地在Servlet和JSP之间传递数据,实现数据模型的封装和复用。 4. **Mysql**:MySQL是一个开源的关系型数据库管理系统,具有高性能、易用性等特点。在本系统中,Mysql用于存储和管理...
《基于JSP+javabean+mysql的在线考试系统详解》 在线考试系统作为一种现代教育技术的产物,已经成为教育领域的重要工具,它以其便捷、高效的特点深受师生喜爱。本系统运用了Java Web技术中的JSP(JavaServer Pages...