i created a java applet class which reads data from a db and display then in an applet. the program runs correctly and i fetches the data when i run it from Eclipse IDE, but when i run it from the browser using an html code it runs but does not get data from the db. this is the code i used.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
public class Driver extends JApplet {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private JLabel lbl = null;
private String label;
public void init() {
try {
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/jdbc?"
+ "user=root&password=");
// statements allow to issue SQL queries to the database
statement = connect.createStatement();
// resultSet gets the result of the SQL query
resultSet = statement
.executeQuery("select * from jdbc.info");
// writeResultSet(resultSet);
while (resultSet.next()) {
String user = resultSet.getString("id");
String name = resultSet.getString("name");
label = user +" "+ name;
}
} catch (Exception e) {
throw e;
} finally {
// close();
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
lbl = new JLabel(label);
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
public static void main(String[] args) throws Exception {
Driver dao = new Driver();
dao.init();
}
}
And this is the HTML code
<html>
<head>
<title>My first app reding from db</title>
</head>
<body>
My first app reding from db<br />
<applet code="jdbcdemo/Driver.class" width="700" height="700" />
</body>
</html>
can anyone help me to find why the applet is not reading from the database which is on a local wamp server. is their a specific directory where i have to add the .Class in order for it to run?
e.printStacktrace();just underthrow e;and open Java console (from control panel i think) before you start the applet