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

jsp 连接 mysql 数据库测试

2014-07-14 16:44 393 查看
一、建立数据库(脚本如下:)

create database javaee;
use javaee;
create table news_inf
(
news_id int primary key auto_increment,
news_title varchar(255)
);

insert into news_inf values
(null , 'Java中国风'),
(null , 'hanshilei.3322.org');





二、connDb.jsp 代码如下:

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 小脚本测试 </title>
<meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<%
//注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/javaee","root","8656216");
//创建Statement
Statement stmt = conn.createStatement();
//执行查询
ResultSet rs = stmt.executeQuery("select * from news_inf");
%>
<table bgcolor="#eee" border="1" width="300">
<%
//遍历结果集
while(rs.next())
{%>
<tr>
<!-- 输出结果集 -->
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<%}%>
<table>
</body>
</html>
运行结果:




注意:别忘记 导入 mysql 驱动包 mysql-connector-java-5.1.17-bin.jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: