我試圖從一個DB調用一大堆JSP腳本來調用,然後我知道這不是一個好主意,所以現在我有點迷路了。將.java的結果寫入jsp
FirstExample.java:
package p;
//STEP 1. Import required packages
import java.sql.*;
public class FirstExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "oracle.jdbc.OracleDriver";
static final String DB_URL = "url";
// Database credentials
static final String USER = "user";
static final String PASS = "pw";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("oracle.jdbc.OracleDriver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
// String sql;
ResultSet rs = stmt.executeQuery("some query");
System.out.println(rs);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
// int id = rs.getInt("some column");
System.out.println (rs.getString(1) + "," + rs.getString(2));
// int age = rs.getInt("some age");
// String first = rs.getString("first");
// String last = rs.getString("last");
//Display values
// System.out.print("some column: " + id);
// System.out.print(",some age: " + age);
// System.out.print(", First: " + first);
// System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
我需要這個結果反饋到JDBCQuery2.jsp,其內容爲:
<%@ page language="java" import="p.FirstExample" %>
<% FirstExample object = new FirstExample();
//FirstExample.init();
%>
<h3>Connection ok</h3>
這正確加載包,但我需要的結果在.java中的方法和我不知道如何使用上面的init調用方法。我應該使用init嗎?最後,我想將java引用與ajax調用關聯起來,但現在誰能幫我把查詢結果放到我的JSP中?任何幫助將不勝感激