Results 1 to 2 of 2
  1. #1
    ibanez270dx is offline Member
    Join Date
    Nov 2007
    Location
    Bay Area, CA
    Posts
    13
    Rep Power
    0

    Default Connecting to DB from Applet

    Hi,
    I'm having trouble connecting to my MySQL database from an applet. Here's the deal: I'm running on a virtual server, my page with the applet is located in such a way that it is accessible from cPanel®, JDBC is installed (and confirmed working), but I cannot connect to my database. I've tried various ways, but I keep getting the exception:

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    blah blah blah
    Caused by: java.io.IOException: open HTTP connection failed.
    blah blah blah

    I CAN connect to the DB using Java, but only through a command prompt, using the follow code I got from a tutorial:

    Java Code:
    import java.sql.*;
    
    public class JdbcExample1 {
    
      public static void main(String args[]) {
        Connection con = null;
    
        try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          con = DriverManager.getConnection("jdbc:mysql:///test", "root", "password");
    
          if(!con.isClosed())
            System.out.println("Successfully connected to MySQL server...");
    
        } catch(Exception e) {
          System.err.println("Exception: " + e.getMessage());
        } finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
        }
      }
    }
    However, I just can't seem to get it right from the applet. Here is the relevent code from my applet, starting right after a button is pressed:

    Java Code:
      public void actionPerformed(ActionEvent e) 
          { 
           if (e.getSource() == tailnum_okButton) 
    	 {
              String tailnumber = tailnum_TextField.getText();
    		if (tailnumber.equals(""))
    		  {
    		   tailnum_Label.setForeground(Color.RED);
    		  } 
    		   else
    		  {
    	 	   search_tailnum_Label.setText(tailnumber);
            	   overallLayout.show(cardPanel, "2");
            
     	// Connect to the MySQL Database
    
      		   Connection con = null;
    
              	   try
              	     {
         		      Class.forName("com.mysql.jdbc.Driver").newInstance();
                          con = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=password");
    		      overallLayout.show(cardPanel, "3");
                	     }
                       catch (Exception c)
                         {
                          overallLayout.show(cardPanel, "4");
    		      c.printStackTrace();
    
               	     }
              	   finally
               	     {
                          if (con != null)
                  	        {
                             try
                       	   {
                                con.close ();
                                System.out.println ("Database connection terminated");
                               }
                             catch (Exception c) { /* ignore close errors */ }
                            }
                         }
    		  }
     	  }
            }
           
    
    }
    If anyone can help me, it would be greatly appreciated!!!

    Thanks in advance,
    - Jeff

  2. #2
    ibanez270dx is offline Member
    Join Date
    Nov 2007
    Location
    Bay Area, CA
    Posts
    13
    Rep Power
    0

    Default

    I did some more research, and I have to pack all the drivers into a .jar file to run a DB applet from a web-browser? How do I do this?

Similar Threads

  1. connecting to a database
    By mxb7642 in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 09:39 AM
  2. Connecting to a Web server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:57 PM
  3. Connecting to a database
    By peiceonly in forum New To Java
    Replies: 2
    Last Post: 04-06-2008, 03:28 PM
  4. Who will know like connecting from Websphere
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 04:41 PM
  5. Replies: 4
    Last Post: 06-08-2007, 07:15 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
  •