程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 分別用java Application 及 JSP調用javaBean的例子

分別用java Application 及 JSP調用javaBean的例子

編輯:關於JAVA

//Java bean

//file name: faq.Java

import Java.sql.*;

public class faq {

String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

String sConnStr = "jdbc:odbc:faq";

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);

Statement stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

}catch(SQLException ex) {

System.err.println("aq.executeQuery: " + ex.getMessage());

}

return rs;

}

}

//Java Application

//file name: testfaq.Java

import Java.sql.*;

class getRs

{

public static String Answer;

public static String Subject;

public static String[][] rsArray = new String[9][2];;

public static int n = 0;

getRs(){

try {

faq workM = new faq();

ResultSet oRs = workM.executeQuery("SELECT * FROM faq");

//System.out.println(oRs.getCount());

while (oRs.next()){

System.out.println(oRs.getString("Answer"));

System.out.println(oRs.getString("Subject"));

n=n+1;

}

}catch (SQLException ex)

{

System.out.println("error:"+ex);

}

}

}

public class testfaq {

public static void main(String[] args) {

getRs aa = new getRs();

}

}

//JSP

//file name: testfaq.JSP

這是我的FAQ!

<%@ page contentType="mimeType; charset=gb2312"%>

<%@ page language="java" import="Java.sql.*" %>

<%

faq workM = new faq();

ResultSet RS = workM.executeQuery("SELECT * FROM faq");

String tt;

while (RS.next()) {

tt = RS.getString("Answer");

out.print("

  • " + RS.getString("Subject") + "
  • ");

    out.print("

    " + tt + "
    ");

    }

    RS.close();

    %>

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved