Arifah Azhar
Arifah Azhar

Reputation: 227

mysql and java jdbc call from another class [runtime application]

the problem is due to the **app.java **being called in response to a plugin start i.e in run time environment. i tried addinG "mysql-connector-java-5.1.18-bin" in the runtime-EclipseApplication too but it does NOT work still.

my mysql / java connection works perfectly when stand alone but i can not develope a connection and get an exception when the connection is called from another class.

i have made a class DataAccessHelper.java

        package dataBase;


        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;

        public class DataAccessHelper {
            private Connection connect = null;
            private Statement statement = null;
            private PreparedStatement preparedStatement = null;
            private ResultSet resultSet = null;

            public void readDataBase() throws Exception {
                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/feedback?"
                                    + "user=sqluser&password=sqluserpw");

                    // Statements allow to issue SQL queries to the database
                    statement = connect.createStatement();
                    // Result set get the result of the SQL query
                    resultSet = statement
                            .executeQuery("select * from FEEDBACK.COMMENTS");
                    writeResultSet(resultSet);

                    // PreparedStatements can use variables and are more efficient
                    preparedStatement = connect
                            .prepareStatement("insert into  FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
                    // "myuser, webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
                    // Parameters start with 1
                    preparedStatement.setString(1, "Test");
                    preparedStatement.setString(2, "TestEmail");
                    preparedStatement.setString(3, "TestWebpage");
                    preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
                    preparedStatement.setString(5, "TestSummary");
                    preparedStatement.setString(6, "TestComment");
                    preparedStatement.executeUpdate();

                    preparedStatement = connect
                            .prepareStatement("SELECT myuser, webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
                    resultSet = preparedStatement.executeQuery();
                    writeResultSet(resultSet);

                    // Remove again the insert comment
                    preparedStatement = connect
                    .prepareStatement("delete from FEEDBACK.COMMENTS where myuser= ? ; ");
                    preparedStatement.setString(1, "Test");
                    preparedStatement.executeUpdate();

                    resultSet = statement
                    .executeQuery("select * from FEEDBACK.COMMENTS");
                    writeMetaData(resultSet);

                } catch (Exception e) {
                    throw e;
                } finally {
                    close();
                }

            }

            private void writeMetaData(ResultSet resultSet) throws SQLException {
                //  Now get some metadata from the database
                // Result set get the result of the SQL query

                System.out.println("The columns in the table are: ");

                System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
                for  (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
                    System.out.println("Column " +i  + " "+ resultSet.getMetaData().getColumnName(i));
                }
            }

            private void writeResultSet(ResultSet resultSet) throws SQLException {
                // ResultSet is initially before the first data set
                while (resultSet.next()) {
                    // It is possible to get the columns via name
                    // also possible to get the columns via the column number
                    // which starts at 1
                    // e.g. resultSet.getSTring(2);
                    String user = resultSet.getString("myuser");
                    String website = resultSet.getString("webpage");
                    String summery = resultSet.getString("summery");
                    Date date = resultSet.getDate("datum");
                    String comment = resultSet.getString("comments");
                    System.out.println("User: " + user);
                    System.out.println("Website: " + website);
                    System.out.println("Summery: " + summery);
                    System.out.println("Date: " + date);
                    System.out.println("Comment: " + comment);
                }
            }

            // You need to close the resultSet
            private void close() {
                try {
                    if (resultSet != null) {
                        resultSet.close();
                    }

                    if (statement != null) {
                        statement.close();
                    }

                    if (connect != null) {
                        connect.close();
                    }
                } catch (Exception e) {

                }
            }

        }

when i run this class directly from main.java

    package dataBase;

    public class main {
        public static void main(String[] args) throws Exception {
            DataAccessHelper dao = new DataAccessHelper();
            dao.readDataBase();
        }
    }

it runs perfectly. however, when i try to call in my other class App.java

    package newmodulewizrd.ui;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.sql.SQLException;

    import javax.swing.JFrame;

    import org.eclipse.jdt.core.JavaModelException;

    import dataBase.DataAccessHelper;

    public class App extends JFrame {

        ScreenContainer screen;

        public App(boolean flag)
        {

        }
        public App() throws JavaModelException, ClassNotFoundException, SQLException{
            FlowLayout fl=new FlowLayout();
            this.setLayout(fl);

            screen = new ScreenContainer();
            DataAccessHelper con= new DataAccessHelper();
            try {
                con.readDataBase();
            } catch (Exception e) {
                e.printStackTrace();
            }

            newModuleWizard.bl.Module module = new newModuleWizard.bl.Module();
            screen.addScreen(new Module(module,con),"module");// Screen1
            screen.addScreen(new Description(module,con),"description");
            screen.addScreen(new PPT(module,con), "ppt");
            screen.addScreen(new Role(module,con), "role");
            screen.addScreen(new MI(module,con), "MI");

            this.add(screen);
            this.add(new Controls(screen));

            this.setSize(new Dimension(800,600));
        }

        public static void main(String[] args) throws JavaModelException, ClassNotFoundException, SQLException {
            new App().setVisible(true);

        }


    }

it gives an exception

 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

apparently my problem is the same as

Java / MySQL - How to access connection from another class?

however, i can not benefit from it cuz the solution provided is according to the code provided which is somehow not accessible to be [most probably the link died]

so plz help anyone

Upvotes: 0

Views: 5657

Answers (1)

Arifah Azhar
Arifah Azhar

Reputation: 227

I solved my problem. I'm posting it here so that anyone who is facing same problem can benefit from it too.

You need to add a Classpath reference in the manifest. Follow these simple steps:

  1. add a folder "lib" to your application
  2. place "mysql-connector-java-5.1.18-bin" in lib
  3. now open your "MANIFEST.MF" and go to tab "RUNTIME"
  4. on bottom right, you would see "classpath" ; click "Add"
  5. now add the folder lib [created in step 1] along with the jar file

in this way, whenever a runtime EclipseApplication /OSGi Application is started this jar file is exported along too. So the connectivity would then be available there too.

Upvotes: 3

Related Questions