• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Really in a hurry!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a million! worked like a charm.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it really help me too
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic