您的位置:首页 > 数据库 > MySQL

jsp 连接MySQL实例

2013-12-25 16:14 399 查看
jsp 连接MySQL数据库:

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>

<%@ page language = "java" %>
<%@ page import ="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>数据库测试</title>
</head>
<body>
hello

<table border="1" align="center">
<tr>
<th>书号</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
</tr>
</table>

<% java.util.Date d = new java.util.Date(); %>

Today's date is <%= d.toString() %> and this jsp page worked!

编号    姓名    地址
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String         url = "jdbc:mysql://localhost/test";
String        user = "root";                              //MySQ帐号
String    password = "admin";                             //MYSQL密码
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from test_table";

ResultSet rs=stmt.executeQuery(sql);

while(rs.next()) {
%>

<table border="1" align="">
<tr>
<th><%=rs.getString(1)%></th>
<th><%=rs.getString(2)%></th>
<th><%=rs.getString(3)%></th>
</tr>
</table>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: