hello I got this code working fine
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class connect { public static void main(String[] args) { Connection conn=null; System.out.println("Start."); try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("So far so good."); conn=DriverManager.getConnection("jdbc:mysql://a.b.c.d/db","user","password"); System.out.println("connected."); } catch(Exception e) { e.printStackTrace(); System.out.println("Bad."); } finally { if(conn!=null) { try { conn.close(); System.out.println("close database connection."); } catch(Exception e){/*ignore this exception*/} } } } }
but when I put it in an applet like this it stops working for me.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; //import java.sql.*; //import sun.jdbc.odbc.JdbcOdbcDriver import com.mysql.jdbc.Driver; import java.applet.Applet; import java.awt.Label; import java.awt.Button; import java.awt.TextField; import java.awt.TextArea; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JOptionPane; public class guestbook extends Applet { Connection conn=null; public void init() { //establich connection try { Class.forName("com.mysql.jdbc.Driver"); JOptionPane.showMessageDialog(null,"so far so good"); conn=DriverManager.getConnection("jdbc:mysql://a.b.c.d/db","user","password"); JOptionPane.showMessageDialog(null,"connected"); } catch(Exception e) { JOptionPane.showMessageDialog(null,"error"); } public void destroy() { // close database connection if(conn!=null) { try { conn.close(); } catch(Exception e){/*ignore this exception*/} } } }