Deprecated
Deprecated

Reputation: 163

Get the results of .java into jsp

I was trying to do a ton of JSP scripting to call from a DB, then I learned that it was not a good idea, so now, I'm a little lost.

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

I need to feed the results of this into JDBCQuery2.jsp, which reads:

<%@ page language="java" import="p.FirstExample" %>
<% FirstExample object = new FirstExample();


//FirstExample.init();    

%>
<h3>Connection ok</h3>

This properly loads the package, but I need the results of the method in the .java and I have no idea how to call the method using the init above. Should I be using init? Eventually, I would like to associate the java reference to an ajax call, but for now could anyone help me pull the query results into my JSP? Any help would be greatly appreciated

Upvotes: 1

Views: 464

Answers (1)

Richard Wеrеzaк
Richard Wеrеzaк

Reputation: 1561

Calling a Java main() method from your JSP isn't the way JSP works.

In general, for a basic pure JSP solution, what you want to do is place the connection information in the web.xml. Then you write a servlet that extends the HttpServlet class and accesses that information from the ServletContext in the init() method which you override for this class. Once your servlet is loaded by the container, this initialization occurs. After that, a call on this servlet's service method (e.g. doGet()) can redirect to your JSP. You can then access the connection information in a variety of ways; for example, by accessing the ServletContext attributes via Java scriptlet or the applicationScope via EL.

It sounds complicated but it's basic JSP/Servlet mechanism. You question seems to indicate that you're new to JSP and might need a to run through a tutorial.

There are more sophisticated ways of doing this depending on your environment and what you are trying to do.

Upvotes: 2

Related Questions