Jump to content


Database


  • Please log in to reply
9 replies to this topic

#1 Narayan

Narayan

    Advanced Member

  • Members
  • PipPipPip
  • 86 posts
  • Gender:Male
  • Location:Nepal
  • Interests:i like to be a java programmer

Posted 12 June 2009 - 05:43 AM

HELLOW GUYS I STUCK ON THE DATABASE CONNECTIVITY OF DERBY 10.5
Could any body tell me how to connect Database via JCREATOR .



#2 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 08 October 2009 - 01:31 AM

QUOTE (Narayan @ Jun 11 2009, 10:43 PM) <{POST_SNAPBACK}>
HELLOW GUYS I STUCK ON THE DATABASE CONNECTIVITY OF DERBY 10.5
Could any body tell me how to connect Database via JCREATOR .


I don't know how DERBY compares, but I can help you out in JDBC with MySQL. They sound similar enough, but I want to confirm before I start filling pages up with code.

#3 shiluotang

shiluotang

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 24 October 2009 - 05:48 AM

QUOTE (Narayan @ Jun 12 2009, 01:43 PM) <{POST_SNAPBACK}>
HELLOW GUYS I STUCK ON THE DATABASE CONNECTIVITY OF DERBY 10.5
Could any body tell me how to connect Database via JCREATOR .

Is it the Derby in eclipse ? That one is just a web container for web application,no database! If I was wrong,please correct. I'll appreciate that!

#4 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 24 October 2009 - 05:35 PM

Derby is the database packaged with the JDK. Post your code Narayan, otherwise we'd just be guessing blind what your problem is.

#5 Narayan

Narayan

    Advanced Member

  • Members
  • PipPipPip
  • 86 posts
  • Gender:Male
  • Location:Nepal
  • Interests:i like to be a java programmer

Posted 30 October 2009 - 08:49 AM

QUOTE (Captain Pierce @ Oct 24 2009, 11:05 PM) <{POST_SNAPBACK}>
Derby is the database packaged with the JDK. Post your code Narayan, otherwise we'd just be guessing blind what your problem is.



Actually can u connect mysql 5.x.  J/Connector via jcreator...

#6 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 30 October 2009 - 10:22 PM

...yes

#7 Narayan

Narayan

    Advanced Member

  • Members
  • PipPipPip
  • 86 posts
  • Gender:Male
  • Location:Nepal
  • Interests:i like to be a java programmer

Posted 10 November 2009 - 12:06 PM

QUOTE (Captain Pierce @ Oct 31 2009, 03:52 AM) <{POST_SNAPBACK}>
...yes


But how???

#8 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 12 November 2009 - 06:56 PM

Read this: http://www.javaspecialists.eu/archive/Issue139.html

And to answer the inevitable reply: Derby's just the example, the generic concept applies to all database drivers.

#9 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 14 November 2009 - 01:37 AM

Here is some code [helper methods] that I've been working on with my project partner for a couple weeks, just as a project:

//connect to a database
public boolean openDatabaseConnection(String userName, String password, String server, String database) throws SQLException {
Connection toReturn = null;

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            toReturn = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database, userName, password);
        } catch (Exception ex) {
           javax.swing.JOptionPane.showMessageDialog(this /*assumes that this class is a JFrame, no GUI = null*/, ex.getMessage, ex.getClass().getSimpleName(), javax.swing.JOptionPane.WARNING_MESSAGE);
            return false;
        } return true;
    }

/**
     * Executes a MySQL statement and catches any <code>SQLException</code> that is thrown
     * @param command the MySQL command to execute
     * @param connection the connection (should be a database connection)
     * @return <code>true</code> if the command is successfully executed (no problem was found) or <code>false</code> if otherwise
     */
    public boolean SQLExec(String command, Connection connection) {
        Statement s = null;
        try {
            s = connection.createStatement();
            s.execute(command);
            return true;
        } catch (SQLException ex) {
            generalThrowable(ex);
            return false;
        }
    }

/**
     * Returns a list of table names from the database specified in the <code>Connection</code> parameter
     * @param connection the <code>Connection</code> [to a database] to get the table names from
     * @return <p>an array of <code>String</code> objects that stem from the <code>ResultSet</code></p>
     * <p>&nbsp or <code>null</code> if an exception was thrown or another error occured (such as if the <code>ResultSet</code> was <code>null</code>)
     */
    private ArrayList<String> getTableNames(Connection connection) {
        ResultSet set;
        ArrayList<String> tableNames = new ArrayList<String>(4);
        try {
            set = connection.getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
            while (set.next()) {
                tableNames.add((String) set.getObject(3)); //why is it '3'?...oh well...it always works
            }
            return tableNames;
        } catch (SQLException ex) {
            generalThrowable(ex);
            return null;
        }
    }

/**
     * The number of rows in the specified table
     * @param table the name of the table to get the number of rows [records] from
     * @return the number of rows in the specified table or -1 if a <code>SQLException</code> is thrown
     */
    public int getNumRows(Connection c, String table) {
        try {
            // Select the number of rows in the table
            Statement stmt = c.createStatement();
            ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM " + table);

            // Get the number of rows from the result set
            resultSet.next();
            return resultSet.getInt(1);
        } catch (SQLException e) {
            generalThrowable(e);
            return -1;
        }
    }

    /**
     * A convenience method for
     * <p>&nbsp<code>connection.createStatement().executeQuery("SELECT * FROM " + table);</code>
     * @param table the name of the table to get the information from
     * @param connection the <code>Connection</code> to the database which has this table's data
     * @return a <code>ResultSet</code> which contains all of the data form the specified table
     */
    public ResultSet queryAllData(Connection connection, String table) {
        try {
            // Create a result set containing all data from my_table
            return connection.createStatement().executeQuery("SELECT * FROM " + table);
        } catch (SQLException e) {
            generalThrowable(e);
            return null;
        }
    }

public String[] getColumnNames(Connection c, String table) {
        try {
            // Create a result set
            PreparedStatement ps = c.prepareStatement("SELECT * from " + table + " WHERE 1 = 0"); //demmy stmt

            // Get result set meta data
            ResultSetMetaData rsmd = ps.getMetaData();
            int numColumns = rsmd.getColumnCount();
            String[] columnNames = new String[numColumns];

            // Get the column names; column indices start from 1
            for (int i = 0; i < numColumns; i++) {
                columnNames[i] = rsmd.getColumnName(i + 1);
            }
            return columnNames;
        } catch (SQLException e) {
            generalThrowable(e);
            return null;
        }
    }

#10 Narayan

Narayan

    Advanced Member

  • Members
  • PipPipPip
  • 86 posts
  • Gender:Male
  • Location:Nepal
  • Interests:i like to be a java programmer

Posted 09 December 2009 - 08:02 AM

QUOTE (Nicholas Adri Chandoke @ Nov 14 2009, 08:07 AM) <{POST_SNAPBACK}>
Here is some code [helper methods] that I've been working on with my project partner for a couple weeks, just as a project:

//connect to a database
public boolean openDatabaseConnection(String userName, String password, String server, String database) throws SQLException {
Connection toReturn = null;

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            toReturn = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database, userName, password);
        } catch (Exception ex) {
           javax.swing.JOptionPane.showMessageDialog(this /*assumes that this class is a JFrame, no GUI = null*/, ex.getMessage, ex.getClass().getSimpleName(), javax.swing.JOptionPane.WARNING_MESSAGE);
            return false;
        } return true;
    }

/**
     * Executes a MySQL statement and catches any <code>SQLException</code> that is thrown
     * @param command the MySQL command to execute
     * @param connection the connection (should be a database connection)
     * @return <code>true</code> if the command is successfully executed (no problem was found) or <code>false</code> if otherwise
     */
    public boolean SQLExec(String command, Connection connection) {
        Statement s = null;
        try {
            s = connection.createStatement();
            s.execute(command);
            return true;
        } catch (SQLException ex) {
            generalThrowable(ex);
            return false;
        }
    }

/**
     * Returns a list of table names from the database specified in the <code>Connection</code> parameter
     * @param connection the <code>Connection</code> [to a database] to get the table names from
     * @return <p>an array of <code>String</code> objects that stem from the <code>ResultSet</code></p>
     * <p>&nbsp or <code>null</code> if an exception was thrown or another error occured (such as if the <code>ResultSet</code> was <code>null</code>)
     */
    private ArrayList<String> getTableNames(Connection connection) {
        ResultSet set;
        ArrayList<String> tableNames = new ArrayList<String>(4);
        try {
            set = connection.getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
            while (set.next()) {
                tableNames.add((String) set.getObject(3)); //why is it '3'?...oh well...it always works
            }
            return tableNames;
        } catch (SQLException ex) {
            generalThrowable(ex);
            return null;
        }
    }

/**
     * The number of rows in the specified table
     * @param table the name of the table to get the number of rows [records] from
     * @return the number of rows in the specified table or -1 if a <code>SQLException</code> is thrown
     */
    public int getNumRows(Connection c, String table) {
        try {
            // Select the number of rows in the table
            Statement stmt = c.createStatement();
            ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM " + table);

            // Get the number of rows from the result set
            resultSet.next();
            return resultSet.getInt(1);
        } catch (SQLException e) {
            generalThrowable(e);
            return -1;
        }
    }

    /**
     * A convenience method for
     * <p>&nbsp<code>connection.createStatement().executeQuery("SELECT * FROM " + table);</code>
     * @param table the name of the table to get the information from
     * @param connection the <code>Connection</code> to the database which has this table's data
     * @return a <code>ResultSet</code> which contains all of the data form the specified table
     */
    public ResultSet queryAllData(Connection connection, String table) {
        try {
            // Create a result set containing all data from my_table
            return connection.createStatement().executeQuery("SELECT * FROM " + table);
        } catch (SQLException e) {
            generalThrowable(e);
            return null;
        }
    }

public String[] getColumnNames(Connection c, String table) {
        try {
            // Create a result set
            PreparedStatement ps = c.prepareStatement("SELECT * from " + table + " WHERE 1 = 0"); //demmy stmt

            // Get result set meta data
            ResultSetMetaData rsmd = ps.getMetaData();
            int numColumns = rsmd.getColumnCount();
            String[] columnNames = new String[numColumns];

            // Get the column names; column indices start from 1
            for (int i = 0; i < numColumns; i++) {
                columnNames[i] = rsmd.getColumnName(i + 1);
            }
            return columnNames;
        } catch (SQLException e) {
            generalThrowable(e);
            return null;
        }
    }



Thanks to all you guys for help