Hot questions for Using Applets in mysql

Question:

I have a access database with table named student And in that there's a Id field.I created a Java applet and in that when I input a value for Id in TextField1 in the applet and press Display button,the applet should display the student's other details in TextFields.

Here's my code for the Display button.

if(command=="Dispaly"){
    try{
    int stId=Integer.parseInt(textField1.getText());
        ResultSet result=state.executeQuery("SELECT*FROM student WHERE Id=stId");

But this gives an error as Too few parameters.Expected1.

But if i change the query as

ResultSet result=state.executeQuery("SELECT*FROM student WHERE Id=3");

(That is for the student with id=3) it works fine. What's wrong with my code? Can someone please show it to me?


Answer:

Your query is failing because the database engine has no way of knowing what stId means. You need to pass that value as a parameter like so

PreparedStatement state = conn.prepareStatement("SELECT * FROM student WHERE Id=?");
int stId=Integer.parseInt(textField1.getText());
state.setInt(1, stId);
ResultSet result=state.executeQuery();

Question:

I'm working in NetBeans, and have two projects. One is a JavaApplication project. This consists of an applet and some other classes. A couple of the classes make MySQL connections. When I run the Applet in this project, it works fine.

The second is a WebApplication project. It contains some html files, as well as a .jar of my JavaApplication project, so I can run the applet from the html files. When I run the WebApplication project, I get the ClassNotFoundException for com.mysql.jdbc.Driver at the line in my code reading "Class.forName("com.mysql.jdbc.Driver");"

I can only assume it's something wrong with where I'm adding the Driver, since when I don't run the web project and only run the applet, it works fine. Here is my Services and Projects tabs, please let me know if you detect something wrong:

The database connection is alive and well, I can look into the tables from this view.

Note that I placed the MySQL jar in the Libraries folder for both the JavaApplication and the WebApplication. I get the same result as when I only placed it in the Libraries folder for the JavaApplication.

Any ideas about what to look into?


Answer:

The jar files are library which are obviously to be placed in the Library section. So,the placement of your mysql-library is fine and OK. But, I doubt you are getting the error because of the unpresence of your ClassLoader files for applet_project! Secondly,the driver must be placed correctly which is fine in the Library folder.

public class ClassNotFoundException

is thrown when an application tries to load in a class through its string name using:

The forName method in class Class.

The findSystemClass method in class ClassLoader .

The loadClass method in class ClassLoader.

In your case, it seems that you haven't loaded your applet-based-project's jar file and that's what giving you ClassLoading related error!I suggest you to add the applet_project.jar of your applet_project. Also,I suggest you to delete the database configuration for jdbc:derby://localhost:1527/sample,though it has nothing to do with your question. Also, the database mysql... must be connected by right clicking. Though this can be doen through code only,but alternatively you can try using both manually first and then through code!

Also,feel free to comment if you don't get or achieve what you wanted to.

Question:

I'm working in Netbeans, developing a Java Applet, as well as a few simple HTML pages that take the form of a Netbeans Web Application. I make a MySQL database connection within the code of my applet. This has been working fine for about a day now (I added the necessary .jar) and I have seen changes in the database as a result of testing, both when I attempt to run the applet straight from Netbeans, and when I run the web application from Netbeans and use the applet embedded in my HTML. However, I recently have been getting a ClassNotFoundException after calling the line:

Class.forName("com.mysql.jdbc.Driver");

in my applet code via the Web Application. Note that this line still executes successfully when I run the applet and not the web application. Only accessing the applet in Firefox is giving me these troubles. I tried restarting Netbeans and Firefox, and still no luck.

I apologize for the vagueness of the question, I was just hoping someone else might have experienced this and would know what to do. I'm pretty sure I haven't changed any code that is related to this Exception (given that the Applet still runs smoothly when not HTML-embedded), so that's all the relevant information I can think to give.


Answer:

You need to let applet know about library with jdbc driver. Here is how to do it: Adding a Third Party Library to Java Applet

Question:

I have been adding modifications to a Java application that runs on a server using an applet. On the server, there is just the jar file, and an index.html page that opens the applet.

Now, I am trying to test the new version of the app on my computer before putting it on the production server, and it doesn't work : the application doesn't connect with the MySQL DB (when I just run it from Eclipse it works, it is when I try to run the jar file from my browser that it doesn't work). I tried running the old app on my desktop the same way and it runs just fine, so I guess the problem is in my jar file.

I have opened the 2 jar archives to see what's inside, and here's what I get :

Old jar : Different folders for the different packages of the app bin folder com folder META-INF folder org folder .classpath file .project file java.policy.applet file mysql-connector-java-5.0.8-bin.jar

And now, here is what's inside the new jar :

Different folders for the different packages of the app META-INF folder .classpath file .fatjar file .project file java.policy.applet file mysql-connector-java-5.0.8-bin.jar settings.fatjar file

So my archive doesn't have the bin, com and org folders. It also has 2 additionnal files.

I don't know which settings should I use when exporting my jar in Eclipse to obtain the same content ?

Also, my new jar isn't signed, could this have something to do with my problem of database access ?


Answer:

I'll answer my own question, as I found the solution :

The jar needs to be exported as a runnable jar The option "extract required libraries into jar" must be selected The jar must be signed (self signed works as it is an internal app, so no need to pay a certificate)

Now it works :)