File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Really in a hurry! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Really in a hurry!" Watch "Really in a hurry!" New topic
Author

Really in a hurry!

John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
I have some code that I wrote for an Applet to access an oracle DB via an Applet (Found out that was a Mistake!) Now I know I should write this to go through a Servlet and make my life easier however I am on very tight time contstraints and need some help converting the code from it's applet state to something I can stick onto the webserver and make it work Here is the code if anyone can help me convert it real quick:

import java.sql.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class MccsMailList extends java.applet.Applet {
static final String connect_string =
"jdbc racle:thin:username/pass@host :p ort:SID";

static final String query = "";

Button execute_button;
TextArea output;
Connection conn;

public void init () {
this.setLayout (new BorderLayout ());
Panel p = new Panel ();
p.setLayout (new FlowLayout (FlowLayout.LEFT));
execute_button = new Button ("Query");
p.add (execute_button);
this.add ("North", p);
output = new TextArea (10, 60);
this.add ("Center", output);
}

public boolean action (Event ev, Object arg) {
if (ev.target == execute_button) {
try {
// See if we need to open the connection to the database
if (conn == null) {
// Load the JDBC driver
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
// Connect to the databse
output.appendText ("Connecting to " + connect_string + "\n");
conn = DriverManager.getConnection (connect_string);
output.appendText ("Connected\n");
}
// Create a statement
Statement stmt = conn.createStatement ();
// Execute the query
output.appendText ("Executing query " + query + "\n");
ResultSet rset = stmt.executeQuery (query);
// Dump the result
while (rset.next ())
output.appendText (rset.getString (1) + "\n");
// We're done
output.appendText ("done.\n");
}
catch (Exception e) {
// Oops
output.appendText (e.getMessage () + "\n");
}
return true;
}
else
return false;
}
}
Thank you in advance!

(Marilyn disabled smilies in this post)
[ February 12, 2002: Message edited by: Marilyn deQueiroz ]
Thomas Mcfarrow
Ranch Hand

Joined: Jul 09, 2001
Posts: 137
Below is a simple servlet that will create a text field and a button in html. It will get the users input and pass it to the db code you created. It will then redisplay the form and underneath the form it will display the query results.
Everything will compile if add the import statement for the oracle.jdbc.driver.OracleDriver.
FYI, doInit(), doGet(HttpServletRequest, HttpServletResponse), and doPost(HttpServletRequest, HttpServletResponse) are methods from HttpServlet.

Cheers!
*******************************************

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
// need to import the oracle stuff
public class myServlet extends HttpServlet {
// your global stuff goes here
static final String connect_string = "jdbc racle:thin:username/pass@host ort:SID";
private Connection conn;

// your initialization stuff goes in here
public void init() {

}
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException, java.io.IOException {
resp.setContentType("text/html");
// writes the output
PrintWriter out = resp.getWriter();
String sQueryString = req.getParameter("querystring");
// get the html for a simple form
// just has a textbox that is your query
out.print(getFormTag());
// if the typed something in the textbox
// and its not the first time being displayed
if(sQueryString != null && sQueryString.trim().length() > 0) {
out.print(executeQuery(sQueryString));
}

}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException, java.io.IOException {
// for now just use the doget on posts
doGet(req, resp);
}
private String getFormTag() {
StringBuffer buffer = new StringBuffer();

buffer.append("<html><body>\n");
buffer.append("<form name='form1'>\n");
buffer.append("<input type='text' name='querystring' value=''>\n");
buffer.append("<input type='submit' name='Submit' value='Query' action='post'>\n");
buffer.append("</form>\n");
buffer.append("</body>\n");

return buffer.toString();
}

private String executeQuery(String query) {

StringBuffer output = new StringBuffer();

try {
// See if we need to open the connection to the database
if (conn == null) {
// Load the JDBC driver

// don't have the jar -- throws an error
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
// Connect to the databse
output.append ("Connecting to " + connect_string + "\n");
conn = DriverManager.getConnection (connect_string);
output.append ("Connected\n");
}
// Create a statement
Statement stmt = conn.createStatement ();
// Execute the query
output.append ("Executing query " + query + "\n");
ResultSet rset = stmt.executeQuery (query);
// Dump the result
while (rset.next ())
output.append (rset.getString (1) + "\n");
// We're done
output.append ("done.\n");
}
catch (Exception e) {
// Oops
output.append (e.getMessage () + "\n");
}
return output.toString();
}
}
[ February 12, 2002: Message edited by: Thomas Mcfarrow ]
John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
Thanks a million! worked like a charm.
Darshan Bhavsar
Greenhorn

Joined: Dec 06, 2001
Posts: 26
it really help me too
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Really in a hurry!
 
Similar Threads
JDBC, Oracle Thin Client thru Applet
NoClassDefFoundError
need help. I have problems running the client
access denied (java.util.PropertyPermission oracle.jserver.version read)
NullPointerException At Statement stmt = conn.createStatement();