相关环境:XAMPP 外加 Tomcat 6.0 扩展;JDK 并配置好环境变量;Mysql 里新建数据库,表。

JSP 使用 Mysql 主要是通过 com.mysql.jdbc.Driver 这个 JDBC 驱动,Tomcat 一般都自带的有,~\tomcat\lib\mysql-connector-java-5.1.6-bin.jar 有这个包就可以。新建 jsp-mysql.jsp 页面:

代码高亮由 vim2html而来 :
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <%@ page contentType="text/html;charset=utf-8" %>
3 <%@ page language="java" %>
4 <%@ page import="com.mysql.jdbc.Driver" %>
5 <%@ page import="java.sql.*" %>
6 <html>
7 <head>
8     <title>Untitled</title>
9 </head>
10 <body>
11     <%
12     String url ="jdbc:mysql://localhost/myguestbook";
13     String user="root";
14     String password="root";
15     Connection conn= DriverManager.getConnection(url,user,password);
16     Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
17     String sql="SELECT * FROM gb_content";
18     ResultSet rs=stmt.executeQuery(sql);
19     while(rs.next()) {%>
20     编号:<%=rs.getString(1)%>
21     <%}%>
22     <%rs.close();
23     stmt.close();
24     conn.close();%>
25 </body>
26 </html>

没有差错的话一般就会连接成功的。继续学习 JSP。

Tags: ,

Related posts

Tags: ,.
Home

No Comments Now!

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (required),(will not be published)
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://hanghang.name),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.