如何使用JSP访问MySQL数据库
程序员文章站
2024-02-25 15:25:51
<%@page import="java.sql.*" import ="java.util.*" import ="...
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contenttype="text/html; charset=gb2312"
%>
<html>
<head>
<title>使用mysql数据库</title>
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
</head>
<body>
<p align="center"><u><font size="5" face="华文新魏">使用jsp读取mysql数据库</font></u></p>
<%!
string host="localhost"; //数据库主机
string database="pinghui"; //数据库名
string user="test"; //用户名
string pass=""; //口令
%><% java.sql.connection sqlconn; //数据库连接对象
java.sql.statement sqlstmt; //语句对象
java.sql.resultset sqlrst; //结果集对象
//登记jdbc驱动对象
class.forname ("org.gjt.mm.mysql.driver").newinstance ();
//连接数据库
sqlconn= java.sql.drivermanager.getconnection ("jdbc:mysql://"+host+"/"+database,user,pass);
//创建语句对象
sqlstmt=sqlconn.createstatement (java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only);
//执行sql语句
string sqlquery="select count(*) from comment";
sqlrst=sqlstmt.executequery (sqlquery);
sqlrst.next();
int count=sqlrst.getint(1); //取得总的留言记录数
sqlquery="select * from comment order by userid desc;";
sqlrst=sqlstmt.executequery (sqlquery); //执行sql语句
%>
总共有<%=count%>条留言
<table border="1" width="100%" bordercolorlight="#cc99ff" cellpadding="2" bordercolordark="#ffffff" cellspacing="0">
<tr>
<td align="center"> id</td>
<td align="center">昵称</td>
<td align="center">性别</td>
<td align="center">地址</td>
<td align="center">ip地址</td>
<td align="center">邮编</td>
<td align="center">oicq</td>
<td align="center">icq</td>
<td align="center">电话</td>
<td align="center">留言</td>
<td align="center">时间</td>
</tr>
<% while (sqlrst.next()) { //取得下一条记录 %>
<tr><!--显示记录-->
<td><%=sqlrst.getstring("userid")%></td>
<td><%=sqlrst.getstring("username")%></td>
<td><%=sqlrst.getstring("sex")%></td>
<td><%=sqlrst.getstring("address") %></td>
<td><%=sqlrst.getstring("ip") %></td>
<td><%=sqlrst.getstring("post")%></td>
<td><%=sqlrst.getstring("oicq")%></td>
<td><%=sqlrst.getstring("icq")%></td>
<td><%=sqlrst.getstring("telnumber")%></td>
<td><%=sqlrst.getstring("comment")%></td>
<td><%=sqlrst.getstring("time") %></td>
</tr>
<% } %>
</table>
</body>
<%
//关闭结果集对象
sqlrst.close();
//关闭语句对象
sqlstmt.close ();
//关闭数据库连接
sqlconn.close();
%>
import ="java.util.*"
import ="java.io.*"
contenttype="text/html; charset=gb2312"
%>
<html>
<head>
<title>使用mysql数据库</title>
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
</head>
<body>
<p align="center"><u><font size="5" face="华文新魏">使用jsp读取mysql数据库</font></u></p>
<%!
string host="localhost"; //数据库主机
string database="pinghui"; //数据库名
string user="test"; //用户名
string pass=""; //口令
%><% java.sql.connection sqlconn; //数据库连接对象
java.sql.statement sqlstmt; //语句对象
java.sql.resultset sqlrst; //结果集对象
//登记jdbc驱动对象
class.forname ("org.gjt.mm.mysql.driver").newinstance ();
//连接数据库
sqlconn= java.sql.drivermanager.getconnection ("jdbc:mysql://"+host+"/"+database,user,pass);
//创建语句对象
sqlstmt=sqlconn.createstatement (java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only);
//执行sql语句
string sqlquery="select count(*) from comment";
sqlrst=sqlstmt.executequery (sqlquery);
sqlrst.next();
int count=sqlrst.getint(1); //取得总的留言记录数
sqlquery="select * from comment order by userid desc;";
sqlrst=sqlstmt.executequery (sqlquery); //执行sql语句
%>
总共有<%=count%>条留言
<table border="1" width="100%" bordercolorlight="#cc99ff" cellpadding="2" bordercolordark="#ffffff" cellspacing="0">
<tr>
<td align="center"> id</td>
<td align="center">昵称</td>
<td align="center">性别</td>
<td align="center">地址</td>
<td align="center">ip地址</td>
<td align="center">邮编</td>
<td align="center">oicq</td>
<td align="center">icq</td>
<td align="center">电话</td>
<td align="center">留言</td>
<td align="center">时间</td>
</tr>
<% while (sqlrst.next()) { //取得下一条记录 %>
<tr><!--显示记录-->
<td><%=sqlrst.getstring("userid")%></td>
<td><%=sqlrst.getstring("username")%></td>
<td><%=sqlrst.getstring("sex")%></td>
<td><%=sqlrst.getstring("address") %></td>
<td><%=sqlrst.getstring("ip") %></td>
<td><%=sqlrst.getstring("post")%></td>
<td><%=sqlrst.getstring("oicq")%></td>
<td><%=sqlrst.getstring("icq")%></td>
<td><%=sqlrst.getstring("telnumber")%></td>
<td><%=sqlrst.getstring("comment")%></td>
<td><%=sqlrst.getstring("time") %></td>
</tr>
<% } %>
</table>
</body>
<%
//关闭结果集对象
sqlrst.close();
//关闭语句对象
sqlstmt.close ();
//关闭数据库连接
sqlconn.close();
%>