Package-Applet
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Package-Applet

  1. #1
    Join Date
    May 2012
    Posts
    8

    Package-Applet

    hi guys, i am newbie, i have following code

    AppletOne.java

    [Code]
    import java.sql.*;
    import javax.swing.*;
    import Package_DB.*;

    public class AppletOne extends JApplet {

    /**
    *
    */
    private static final long serialVersionUID = 1L;
    private Connection connection;
    ConnectToDB objConnectToDB = new ConnectToDB();

    public void init() {
    GetData();
    }

    private void GetData() {
    try {
    connection = objConnectToDB.GetConnection("localhost:3306", "test",
    "root", "root@123");
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Throwable e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    String listOfNames = "";
    try {
    listOfNames = GetaDataFromMySQLDataBase();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    JOptionPane.showMessageDialog(null, listOfNames);
    }

    private String GetaDataFromMySQLDataBase() throws SQLException {
    String query = "SELECT * FROM test.test";
    String listOfNames = objConnectToDB.GetDataAsString(connection, query);
    return listOfNames;
    }
    }
    [Code]

    and

    a package

    [Code]
    package Package_DB;

    import java.sql.*;

    public class ConnectToDB {
    private Connection connection;

    public static void main(String args[]) {
    System.out.print("asdasd");
    // JOptionPane.showMessageDialog(null,"listOfNames");
    }

    public Connection GetConnection(String server, String database,
    String userName, String password) throws InstantiationException,
    Exception, Throwable {

    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection("jdbc:mysql://" + server
    + "/" + database + "?user=" + userName + "&password="
    + password);
    return connection;
    } catch (SQLException e) {
    // return e.getMessage();
    return connection;
    }

    }

    public String GetDataAsString(Connection connection, String query)
    throws SQLException {
    Statement statement = connection.createStatement();
    ResultSet rs = statement.executeQuery(query);
    String lastName = "";
    while (rs.next()) {
    lastName += rs.getString("Name") + ",";
    }
    rs.close();
    return lastName;
    }
    }

    [Code]


    while running in a eclipse, i got the data from mySQL database.. but in through command prompt i am getting following error

    C:\Users\Srihari\workspace\exp1\src>javac AppletOne.java

    C:\Users\Srihari\workspace\exp1\src>java AppletOne
    Exception in thread "main" java.lang.NoSuchMethodError: main

    C:\Users\Srihari\workspace\exp1\src>

    and


    C:\Users\Srihari\workspace\exp1\src>appletviewer AppletOne.html
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at Package_DB.ConnectToDB.GetConnection(ConnectToDB.java:18)
    at AppletOne.GetData(AppletOne.java:20)
    at AppletOne.init(AppletOne.java:15)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)
    java.lang.NullPointerException
    at Package_DB.ConnectToDB.GetDataAsString(ConnectToDB.java:32)
    at AppletOne.GetaDataFromMySQLDataBase(AppletOne.java:45)
    at AppletOne.GetData(AppletOne.java:35)
    at AppletOne.init(AppletOne.java:15)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)

    C:\Users\Srihari\workspace\exp1\src>


    and my html page look like

    <html>
    <applet code="AppletOne.class" width="500" height="200"></applet>
    </applet>
    </html>

    i 've windows7 OS and 've only java path..

    but in eclipse i added mysql-connector-java-5.1.6.jar as reference..


    please help me in this...

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Package-Applet

    while running in a eclipse, i got the data from mySQL database.. but in through command prompt i am getting following error
    It's an applet, to run it as an applet you need to run it from a browser or appletviewer. Also you can't run it from the command line as is as there's no main method.

    and my html page look like
    You are missing the archive attribute to specify the name of the mysql-connector-java-5.1.6.jar.

    Look at this guide: http://www.cs.wcupa.edu/~rkline/java/applets.html
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center