關(guān)于eclipse的使用,請(qǐng)參考我的blog其他文章。
在Eclipse3.1中如何配置Lomboz3.1
在Eclipse3.1中如何使用Lomboz3.1開發(fā)JSP
1、首先在MySQL數(shù)據(jù)庫(kù)中建立books數(shù)據(jù)庫(kù),再建book表。
然后插入數(shù)據(jù)。SQL語(yǔ)言如下:
create database books;
use books;
create table book(bookId varchar(50),bookName varchar(50),publisher varchar(100),price float,constraint pk_book primary key(bookId))TYPE=MyISAM,default character set gbk;(加粗字體是我在建表時(shí)就對(duì)默認(rèn)字體進(jìn)行的固定,因?yàn)槲覜]有使用javamxj的在windows中加my.ini文件的做法)
insert into book values('1001','Tomcat與Java Web開發(fā)技術(shù)詳解','電子工業(yè)出版社',45.00);
insert into book values('1002','精通Struts:基于MVC的Java Web設(shè)計(jì)與開發(fā)','電子工業(yè)出版社',49.00);
insert into book values('1003','精通Hibernater:Java對(duì)象持久化技術(shù)詳解','電子工業(yè)出版社',59.00);
insert into book values('1004','精通EJB','電子工業(yè)出版社',59.00);
insert into book values('1005','J2EE應(yīng)用與BEA Weblogic Server','電子工業(yè)出版社',56.00);
2、接著在eclipse3.1中建立一個(gè)名為MySQL的項(xiàng)目:
3、建議mysql.jsp文件。輸入以下代碼:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<html>
<body>
以下是從MySQL數(shù)據(jù)庫(kù)讀取的數(shù)據(jù):<hr>
<table border=1>
<tr><td>ID</td><td>書名</td><td>出版社</td><td>價(jià)格</td></tr>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/BookDB?useUnicode=true&characterEncoding=GBK","t14cwf","cwf");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from book");
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("bookId")+"</td>");
out.println("<td>"+rst.getString("bookName")+"</td>");
out.println("<td>"+rst.getString("publisher")+"</td>");
out.println("<td>"+rst.getFloat("price")+"</td>");
out.println("</tr>");
}
//關(guān)閉連接、釋放資源
rst.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>
4、將mysql-connector-java-3.1.10-bin.jar文件復(fù)制到tomcat\common\lib
5、可以在eclipse中運(yùn)行jsp文件了。
6、運(yùn)行結(jié)果:

很多人連接不上數(shù)據(jù)庫(kù)最多的原因還在于web.xml文件問題。
一下是lomboz自動(dòng)生成的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
MySQL</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>