I am new to java programming and I just created a simple program that connects to a mysql database to update a Jtable right on my laptop. Now I want to install the program on my friends laptop and we want to share the same database. Do I have to configure the Mysql database or I would just change the connection code in my friend's copy of the program? Here is a copy of my code:

    public class dataAccess {

    public static void main(String args[]){
        Connection connection = null; //connection manager
        Statement statement = null;// query statement
        //connect to database books and query database
        try{
            Class.forName(Driver); // load database driver class
            //establish connection to database
            connection = DriverManager.getConnection(url,"root", "blessme" );

            //create Statement for querying database
            statement = connection.createStatement();
            //query database
            ResultSet resultSet = statement.executeQuery("select ID, name, dept, salary from testing");
            //process query results
            ResultSetMetaData metaData = resultSet.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            System.out.println("Staff in the testing table database");


            for ( int i = 1; i <= numberOfColumns; i++ )
            System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
            System.out.println();

            while (resultSet.next()){
                for ( int i = 1; i <= numberOfColumns; i++ )
                    System.out.printf( "%-8s\t", resultSet.getObject( i ) );
                    System.out.println();
                } // end while
        }catch(SQLException sqlException ){
            sqlException.printStackTrace();
            System.exit( 1 );
        }catch ( ClassNotFoundException classNotFound ){
            classNotFound.printStackTrace();
            System.exit( 1 );
    }
    finally // ensure statement and connection are closed properly
            {                                                             
                try{                                                          
                    statement.close();                                      
                    connection.close();                                     
                } // end try                                               
                catch ( Exception exception )                              
                {                                                          
                    exception.printStackTrace();                            
                    System.exit( 1 );                                       
                } // end catch                                             
            } // end finally 
        inserter();
    }
    public static void inserter(){
        Connection connection = null; //connection manager
        Statement statement2 = null;// query statement
        System.out.println("now at inserter");
        //connect to database books and query database
        try{
            Class.forName(Driver); // load database driver class
            //establish connection to database
            connection = DriverManager.getConnection(url,"root", "blessme" );

            String insertSql = "insert into testing(ID, name, dept, salary) values(?, ?, ?, ?)";
            //create Statement for querying database
            statement2 = connection.prepareStatement(insertSql);
            //query database
            ResultSet resultSet2 = statement2.executeQuery("insert into testing(ID, name, dept, salary) values(3, Joshua," +
                    " Programs, 300000)");
        }
        catch(SQLException sqlException ){
            sqlException.printStackTrace();
            System.exit( 1 );
        }catch ( ClassNotFoundException classNotFound ){
            classNotFound.printStackTrace();
            System.exit( 1 );
        }
        finally // ensure statement and connection are closed properly
        {                                                             
            try{                                                          
                statement2.close();                                      
                connection.close();                                     
            } // end try                                               
            catch ( Exception exception )                              
            {                                                          
                exception.printStackTrace();                            
                System.exit( 1 );                                       
            } // end catch                                             
        } // end finally   
    }
}

Recommended Answers

All 2 Replies

create a webservice, send a request to that service, let that service deal with the database

or use a connect string that's independent of where you're connecting from...

Of course that assumes you've a way to set up a database that can even be reached from outside your home network, have it properly secured and running, and can rely on to remain running.
But of course if you go with a web service that talks to the database you have the same considerations for the application server that runs the web service.

btw,

  • "Class.forName(xxx)" is no longer needed with JDBC.
  • Avoid all that ugly code duplication
  • For any realistic application you're going to want to use a connection pool
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.