APPLET MYsql


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: APPLET MYsql

  1. #1
    Join Date
    Jun 2006
    Posts
    2

    APPLET MYsql

    Hello,

    I am trying to write an applet that talks to a mySQL. I have a program that works as a stnd alone program but when I put it in an applet, I get the following error:

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    Below is my code? java then html

    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;

    public class Loom extends Applet implements ActionListener{

    Connection dbConn; // Setup a connection with the OS
    Statement dbCmdText; // Contains SQL commands
    String dbSQL; // Temp String for building SQL command
    ResultSet dbRecordset; //
    ResultSet dbQuerySet;
    JTextField nameField;
    JButton button1;
    JButton button2;
    private String [] rowstr ={};
    private String [] qtystr ={};
    private String [] casostr ={};
    private String [] vintstr ={};
    private String [] winestr ={};
    private String [] descstr ={};
    private String [] ownstr ={};
    private String [] inty = {};
    // private String [] casostr ={};

    private JComboBox cboInventory = new JComboBox();
    // JButton button3;



    public void init() {
    try {
    createGUI();


    }
    catch (Exception e) {
    System.err.println("createGUI didn't successfully complete");
    }
    // casostr = new String[55];
    // qtystr = new String[55];
    // vintstr = new String[55];
    winestr=new String[55];
    // descstr=new String[55];
    // ownstr=new String[55];

    try{
    String driver = "com.mysql.jdbc.Driver";
    System.out.println( "=> loading driver:" );
    Class.forName( driver );
    System.out.println( "OK" );
    String strConn;
    System.out.println("Attempting to establish a database connection");
    try
    {
    // Generic SUN driver for databases
    String ibid = "jdbc:mysql://localhost/test";
    dbConn = DriverManager.getConnection(ibid, "root", "r");
    // if you cannot find the database, handle it here
    if (dbConn == null) // Is the Connection working?
    {
    System.out.println("Connection to database failed");
    }
    }
    catch (Exception e)
    {
    int q =1;
    String pedro =winestr[q];
    nameField = new JTextField(40);
    add(nameField);
    nameField.setText("cripees");

    }

    String gooey="SELECT * FROM hag";
    dbCmdText = dbConn.createStatement();
    dbRecordset = dbCmdText.executeQuery(gooey);
    ResultSetMetaData rsmd = dbRecordset.getMetaData();
    int k=50;
    int w=0;
    while (dbRecordset.next())
    {
    rowstr[w]=dbRecordset.getString("row_id");
    casostr[w]=dbRecordset.getString("caso");
    qtystr[w]=dbRecordset.getString("qty");
    vintstr[w]=dbRecordset.getString("vintage");
    winestr[w]=dbRecordset.getString("winery");
    descstr[w]=dbRecordset.getString("description");
    ownstr[w]=dbRecordset.getString("owner");
    System.out.println();
    w++;

    } // end of while
    }
    catch (Exception e)
    {
    int q =1;
    String pedro =winestr[q];
    nameField = new JTextField(30);
    add(nameField);
    nameField.setText(e.toString());
    // System.err.println(e.toString());
    } // end of catch


    }
    private void createGUI() {



    setLayout(new FlowLayout());
    add (button1 = new JButton("Load Wine List"));
    add (button2 = new JButton("Delete this Wine"));
    // add (button3 = new JButton("Add a Wine to the Database"));

    button1.addActionListener(this);
    button2.addActionListener(this);
    cboInventory = new JComboBox();
    cboInventory.addActionListener(new MomboListener());
    add(cboInventory);
    String sqlString;

    }


    public void actionPerformed(ActionEvent evt)
    {
    // Here we will ask what component called this method
    if (evt.getSource() == button1)
    // So it was the okButton, then let's perform his actions
    // Let the applet perform Paint again.
    // That will cause the aplet to get the text out of the textField
    // again and show it.
    // repaint();
    System.out.println("Not here!");

    // Actions of the wrongButton
    else if (evt.getSource() == button2)
    {

    // Change the text on the button for fun

    button2.setLabel("Not here!");
    // Changes the text in the TextField
    nameField.setText("That was the wrong button!");
    // Lets the applet show that message.
    // repaint();
    }
    }
    private class MomboListener implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    Object goose = event.getSource();
    if (goose == cboInventory){
    System.out.println("OK");
    }
    }
    }

    public void destroy()
    {
    System.out.println("destroy method called");
    }
    }

    HTML

    <HTML>
    <Head>
    <Title>Wine Storage</Title>
    <Body>
    <P> <center><Applet Code="Loom.class" align="center" width="850" height="750" VSPACE="50"
    </Applet> </P>
    </Body>
    </HTML>

    Any ideas

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    That is because the class com.mysql.jdbc.Driver is not part of the applets "luggage". For an applet to talk directly to a remote database server (ie. conneting to the db-server port), you would have to make adjustments to the accessibility of that server, and from what I have heard, that is not recommended from a security point of view.

    A better solution would be to have a layered access to the db-server; a java servlet, a perl script or a php script at the server-side that took an SQL query as a request, accessed the database and returned the resultset as a char stream.
    eschew obfuscation

  3. #3
    Join Date
    Mar 2006
    Posts
    35
    You definately need to use servlets you definately don't want to change the accessibility of the server unless you want someone hacking away at your files.

  4. #4
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    the java.lang.ClassNotFoundException: com.mysql.jdbc.Driver comes from the missing mysqlj.jar. do the following:
    extract the contents of mysqlj.jar and pack them with your applet together in an jar file.

Similar Threads

  1. JavaMail applet problem
    By Tataroz T. in forum Java
    Replies: 1
    Last Post: 10-20-2005, 06:19 AM
  2. Bouncing Ball Applet
    By nescafe in forum Java
    Replies: 1
    Last Post: 10-07-2005, 08:51 AM
  3. MySql help Request
    By airrazor in forum Database
    Replies: 6
    Last Post: 10-04-2005, 04:00 PM
  4. Replies: 3
    Last Post: 08-23-2001, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center