Applets and Databases
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jijesh_Kumar
Posted On:   Monday, March 22, 2004 10:12 AM

I have an applet program in which I have to connect to database on a button click. The program is working fine in appletviewer. But I am not getting the result in browser. Please help me in solving this problem... The code is as follows... import javax.swing.*; import javax.swing.event.*; import java.applet.*; import java.awt.*; import java.awt.event.*; public class DApplet extends JApplet implements ActionListener { static Connection Conn; Statement st; ResultSet rs; JTextArea jt; JButton jb; JPanel jp; public void init() { jt=new JTextArea(20,20); jb=new JButton("Click");    More>>

I have an applet program in which I have to connect to database on a button click. The program is working fine in appletviewer. But I am not getting the result in browser.
Please help me in solving this problem...
The code is as follows...


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


public class DApplet extends JApplet implements ActionListener
{

static Connection Conn;
Statement st;
ResultSet rs;
JTextArea jt;
JButton jb;
JPanel jp;

public void init()
{
jt=new JTextArea(20,20);
jb=new JButton("Click");
jp=new JPanel();
jp.add(jb);
jp.add(jt);
getContentPane().add(jp,BorderLayout.CENTER);
jb.addActionListener(this);
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E)
{
System.err.println("Unable to load driver.");
E.printStackTrace();
}

try
{
Conn = DriverManager.getConnection("jdbc:mysql://localhost/aict");
}
catch (SQLException E)
{
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
}
public void actionPerformed(ActionEvent ae)
{
try
{
st=Conn.createStatement();
rs=st.executeQuery("SELECT * FROM verb");
while(rs.next())
{
jt.append("HelloWorld"+"
");
}
rs.close();
Conn.close();
}catch(SQLException e){}
}
}

   <<Less

Re: Applets and Databases

Posted By:   Jerry_Lampi  
Posted On:   Monday, March 22, 2004 02:39 PM



I personally have never done any DB in applet, but here's the code you posted cleaned up (using tags as described by the Verify button in JGuru), so someone else may be able to help:

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

class DApplet extends JApplet implements ActionListener {
static Connection Conn;
Statement st;
ResultSet rs;
JTextArea jt;
JButton jb;
JPanel jp;

public void init() {
jt = new JTextArea(20, 20);
jb = new JButton("Click");
jp = new JPanel();
jp.add(jb);
jp.add(jt);
getContentPane().add(jp, BorderLayout.CENTER);
jb.addActionListener(this);
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch(Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try {
Conn = DriverManager.getConnection("jdbc:mysql://localhost/aict");
} catch(SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
}

public void actionPerformed(ActionEvent ae) {
try {
st = Conn.createStatement();
rs = st.executeQuery("SELECT * FROM verb");
while(rs.next()) {
jt.append("HelloWorld" + "
");
}
rs.close();
Conn.close();
} catch(SQLException e) {
}
}
}
About | Sitemap | Contact