费了一天,试了N次,终于可以用JSP连接MYSQL数据库了,下面是我的详细步骤:
1、我的配置如:Java SDK 1.5.0_09+Apache Tomcat/5.5.20+MYSQL 5.0.26
首先安装JAVA SDK,我的安装目录为E:/Java/jdk1.5.0_09;然后配置系统变量,我的为:Path=%SystemRoot%/system32;%SystemRoot%;%SystemRoot%/System32/Wbem;D:/Program Files/Microsoft SQL Server/80/Tools/BINN;%JAVA_HOME%/bin;%JAVA_HOME%/jre/bin;E:/Java/MySQL/MySQL Server 5.0/bin
CLASSPATH=.;%JAVA_HOME%/lib;%JAVA_HOME%/lib/tools.jar;%TOMCAT_HOME%/common/lib;%JAVA_HOME%/lib/dt.jar (记住一定要加.;表当前目录)
2、然后安装Tomcat/5.5.20,我的安装目录为:E:/Java/Tomcat 5.5;配置的变量上面已经很清楚了;
3、安装MYSQL5.0.26,记住下载MYSQL 的JDBC驱动,我的为mysql-connector-java-3.1.10-bin.jar;在搜索里面查一下这个文件名就可以下了,然后复制到E:/Java/Tomcat 5.5/common/lib下,一般都可以的。
现在就可以写一个文件来测试一下:
<%@ page language="java" import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<%
String url="jdbc:mysql://localhost/liuyanben";
String userName="root";
String password="root";
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 liuyan_temp";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
%>
<div><%=rs.getString(3)%></div>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
数据库文件jackie_yang.sql
DROP DATABASE `jackie_yang`;
CREATE DATABASE `jackie_yang`;
USE jackie_yang;
CREATE TABLE liuyan_temp (
id int(6) NOT NULL auto_increment,
url varchar(100) default '#',
ip 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 liuyan_temp values(1,'http://rainren.54bk.com','565491d704013245','rainrenamy@gmail.com','1102501','天地网络--JSP留言薄','I come fro Hunan','jackie yang');
insert into liuyan_temp values(2,'http://rainren.54bk.com','565491d704013245','rainrenamy@gmail.com','1102501','天地网络--JSP留言薄','I come fro Hunan','jackie yang');
insert into liuyan_temp values(3,'http://rainren.54bk.com','565491d704013245','rainrenamy@gmail.com','1102501','天地网络--JSP留言薄','I come fro Hunan','jackie yang');
在mysql命令行下使用source e:/jackie_yang.sql 数据库就建立了
现在在浏览器里试一下!