1.首先安装MySql数据库,并将数据库驱动程序文件mysql-connector-java-3.1.12-bin.jar放在Tomcat的common/lib中。2.在MySQL的登陆用户名:root,密码:mysql建立数据库testdb,3.表user 基本代码如下: <%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.sql.Statement" import="java.sql.*" %><html><head><title>helloworld.jsp</title></head><body><% try{ Class.forName("org.gjt.mm.mysql.Driver"); out.println("加载Mysql Driver成功!<br>"); } catch(Exception e) { out.println("加载Mysql Driver失败!<br>"); e.printStackTrace(); } try{ out.println("开始连接Mysql server!<br>"); Connection connect=DriverManager.getConnection("jdbc:mysql://localhost/testdb?user=root&password=mysql&useUnicode=true&characterEncoding=8859_1"); //jdbc:mysql://localhost/testdb?user=root&password=mysql&useUnicode=true&characterEncoding=8859_1 //jdbc:mysql://localhost/数据库名user=数据库登陆用户名&password=数据库登陆密码&useUnicode=是否允许设置字符编码&characterEncoding=允许设置编码时,要设置的编码; out.print("成功连接Mysql server!<br><br>"); Statement stmt = connect.createStatement(); ResultSet rs=stmt.executeQuery("select * from user"); out.print("读取数据如下:<br>"); while(rs.next()) { out.println(rs.getInt(1)); out.println(rs.getString(2)+"<br>"); } } catch(Exception e) { out.print("获得数据错误!"); e.printStackTrace(); } %></body></html> 4.注意:如果只写import="java.sql.*" ,不写import="java.sql.Statement" 则在Statement stmt = connect.createStatement();其中Statement下面显示是红色波浪线,程序调用数据库等一切正常。