最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

如何使用JSP访问MySQL数据库

日期:10-24    来源:中国设计秀    作者:cnwebshow.com

<%@page import="java.sql.*" eFN中国设计秀
import ="java.util.*" eFN中国设计秀
import ="java.io.*" eFN中国设计秀
contentType="text/html; charset=gb2312" eFN中国设计秀
%> eFN中国设计秀
<html> eFN中国设计秀
<head> eFN中国设计秀
<title>使用MySQL数据库</title> eFN中国设计秀
<meta name="GENERATOR" content="Microsoft FrontPage 4.0"> eFN中国设计秀
<meta name="PRogId" content="FrontPage.Editor.Document"> eFN中国设计秀
</head> eFN中国设计秀
<body> eFN中国设计秀
<p align="center"><u><font size="5" face="华文新魏">使用jsp读取Mysql数据库</font></u></p> eFN中国设计秀
<%! eFN中国设计秀
String host="localhost"; //数据库主机 eFN中国设计秀
String database="pinghui"; //数据库名 eFN中国设计秀
String user="test"; //用户名 eFN中国设计秀
String pass=""; //口令 eFN中国设计秀
%><% java.sql.Connection sqlConn; //数据库连接对象 eFN中国设计秀
java.sql.Statement sqlStmt; //语句对象 eFN中国设计秀
java.sql.ResultSet sqlRst; //结果集对象 eFN中国设计秀
//登记JDBC驱动对象 eFN中国设计秀
Class.forName ("org.gjt.mm.mysql.Driver").newInstance (); eFN中国设计秀
//连接数据库 eFN中国设计秀
sqlConn= java.sql.DriverManager.getConnection ("jdbc:mysql://"+host+"/"+database,user,pass); eFN中国设计秀
//创建语句对象 eFN中国设计秀
sqlStmt=sqlConn.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY); eFN中国设计秀
//执行Sql语句 eFN中国设计秀
String sqlQuery="select count(*) from comment"; eFN中国设计秀
sqlRst=sqlStmt.executeQuery (sqlQuery); eFN中国设计秀
sqlRst.next(); eFN中国设计秀
int count=sqlRst.getInt(1); //取得总的留言记录数 eFN中国设计秀
sqlQuery="select * from comment order by userid desc;"; eFN中国设计秀
sqlRst=sqlStmt.executeQuery (sqlQuery); //执行SQL语句 eFN中国设计秀
%> eFN中国设计秀
总共有<%=count%>条留言 eFN中国设计秀
<table border="1" width="100%" bordercolorlight="#CC99FF" cellpadding="2" bordercolordark="#FFFFFF" cellspacing="0"> eFN中国设计秀
<tr> eFN中国设计秀
  <td align="center">&nbsp;ID</td> eFN中国设计秀
  <td align="center">昵称</td> eFN中国设计秀
  <td align="center">性别</td> eFN中国设计秀
  <td align="center">地址</td> eFN中国设计秀
  <td align="center">ip地址</td> eFN中国设计秀
  <td align="center">邮编</td> eFN中国设计秀
  <td align="center">Oicq</td> eFN中国设计秀
  <td align="center">ICQ</td> eFN中国设计秀
  <td align="center">电话</td> eFN中国设计秀
  <td align="center">留言</td> eFN中国设计秀
  <td align="center">时间</td> eFN中国设计秀
</tr> eFN中国设计秀
<% while (sqlRst.next()) { //取得下一条记录 %> eFN中国设计秀
<tr><!--显示记录--> eFN中国设计秀
  <td><%=sqlRst.getString("userid")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("username")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("sex")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("address") %></td> eFN中国设计秀
  <td><%=sqlRst.getString("ip") %></td> eFN中国设计秀
  <td><%=sqlRst.getString("post")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("oicq")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("icq")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("telnumber")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("comment")%></td> eFN中国设计秀
  <td><%=sqlRst.getString("time") %></td> eFN中国设计秀
</tr> eFN中国设计秀
<% } %> eFN中国设计秀
</table> eFN中国设计秀
</body> eFN中国设计秀
<% eFN中国设计秀
//关闭结果集对象 eFN中国设计秀
  sqlRst.close(); eFN中国设计秀
  //关闭语句对象 eFN中国设计秀
sqlStmt.close (); eFN中国设计秀
//关闭数据库连接 eFN中国设计秀
sqlConn.close(); eFN中国设计秀
%> eFN中国设计秀
 eFN中国设计秀

本文引用地址:/bc/article_46772.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明