---
title: "JSP连接MySQL实例"
author: "wtgame"
date: "2010-12-24T03:24:04Z"
tags: []
categories: ["非技术"]
slug: "jsp-mysql"
draft: false
lastmod: "2010-12-24T03:27:21Z"
id: 850769
url: "https://wtgame.iteye.com/blog/850769"
---

```
CREATE TABLE bukee (
  id int(6) NOT NULL auto_increment,
  url varchar(100) default '#',
  name varchar(20) default NULL,
  email varchar(50) default '#',
  qq varchar(20) default NULL,
  sj varchar(30) default NULL,
  content text,
  xm varchar(20) default '没有姓名',
  KEY id (id)
) TYPE=MyISAM;
insert into bukee values(1,'http://www.bukee.com.cn','虎胆闯天下','xujiemusic@126.com','89559827','中国龙留言薄','I come fro Hunan','xujie');
insert into bukee values(2,'http://www.bukee.com.cn','宇宙浪子','xujiemusic@126.com','89559827','中国龙留言薄','I come fro Hunan','xujie');
insert into bukee values(3,'http://www.bukee.com.cn','虎胆闯天下','xujiemusic@126.com','89559827','中国龙留言薄','I come fro Hunan','xujie');
```

 

 

```
<%@ page language="java" import="java.sql.*"  contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>数据库连接</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body bgcolor="#FFFFFF">
<%
String url="jdbc:mysql://localhost/test";
String userName="root";
String password="1q2w3e";
String sql=null;
Connection conn=null;
Statement stmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
  System.out.println("加载驱动时出现异常");
}
conn=DriverManager.getConnection(url,userName,password);
stmt=conn.createStatement();
sql="select * from bukee";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
%>
<div><%=rs.getString(3)%></div>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
```

 

```
数据库连接----这个是标题
虎胆闯天下
宇宙浪子
虎胆闯天下
```