1 下载安装mysql数据库,设定root用户密码为:123456
2 建立test 数据库
create database test;--运行
use test;--运行
3 建立表admin
create table admin (id int(4) auto_increment primary key,name varchar(20));--运行
INSERT INTO admin (name) VALUES(´csufuyi´);--运行
4,在tomcat webapps 下面建立test目录,内建文件test.jsp内容如下:
测试代码如下:
<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="java.sql.*"%>
<%
Connection conn = null;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456");
if(conn==null){
System.out.println("get Conn Error");
}
Statement stmt=conn.createStatement();
ResultSet RS_result=null;
%>
<html>
<head>
<title>ѧϰ</title></head>
<body>
<%
RS_result=stmt.executeQuery("select * from admin");
String Name;
while(RS_result.next())
{
Name=RS_result.getString("name");
%>
<%=Name%>
<%
}
RS_result.close();
stmt.close();
conn.close();
%>
</body>
</html>
5下载最新数据库驱动拷贝到D:/tomcat/common/lib
下,如果驱动版本与数据库版本不匹配,将会出现,
Communication failure during handshake. Is there a server running on localhost:3306?
的错误,更换到最新的驱动即可!
6start up tomcat ,ok ,there is a "csufuyi"in the screen,you are succeed!
Good Luck!