-----------------faq.java
//java bean
//file name: faq.java
package Test;
import java.sql.*;
public class faq {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:wz";
Connection conn = null;
ResultSet rs = null;
public faq() {
try {
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e) {
System.err.println("faq(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"liuyz","liuyz1");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
----------------------Hello.java
//java bean
//file name: faq.java
package Test;
public class Hello {
String h="test";
public void setHello(String s)
{
h=s;
}
public String getHello()
{
return h;
}
public static void main (String[] args)
{
Hello ho= new Hello();
System.out.println(ho.h);
}
}
---------------------testfaq2.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的FAQ !</title>
</head>
<body>
<p><b>这是我的FAQ!</b></p>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="Hello1" scope="session" class="Test.Hello" />
<%
String str = "Hello,欢迎学习JSP!";
Hello1.setHello(str);
out.println(Hello1.getHello() + "<br>");
%>
<jsp:useBean id="mybean" scope="session" class="Test.faq" />
<%
ResultSet RS = mybean.executeQuery("SELECT * FROM work_log");
String tt;
while (RS.next()) {
tt = RS.getString("proc_name");
out.print("<LI>" + RS.getString("begin_time") + "</LI>");
out.print("<pre>" + tt + "</pre>");
}
RS.close();
%>
</body>
</html>
1、javac -d . faq.java ->faq.class
javac -d . Hello.java ->Hello.class
2、把faq.class,Hello.class放于Tomcat 6.0\webapps\ROOT\WEB-INF\classes\Test下
3、编辑testfaq2.jsp,调用javabean访问数据库