Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2006
    Posts
    41

    Unanswered: Need help connecting to database on a networked pc

    hi,

    my java code currently connects to a mySQL database on the same pc but I need the java code to be able to connect to a mySQL database on another pc on the same network.

    My current code to connect to the database is:



    Code:
    package DataBaseConnection;
    
    
    import java.sql.*;
    import java.util.ArrayList;
    
    
    /**
     *
     * @author Nick
     */
    public class MarksDatabase {
        
        Statement stmt;
        ResultSet rs;
        Connection conn;
        
        /** Creates a new instance of TestDatabase */
        public MarksDatabase() 
        {
            
            conn = null;
            
            try
            {
                   String userName = "msc89nxv";
                   String password = "n0121429";
                   String url = "jdbc:mysql://localhost/test_database";
    
                   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                   conn = DriverManager.getConnection (url, userName, password);
                   System.out.println ("Database connection established");
                   
             }
             catch (Exception e)
             {
                   System.err.println("Cannot connect to database server");
             }
             
        }
        
        public void closeConnection()
        {
            if(conn != null)
            {
                try
                {
                    conn.close();
                }
                catch(Exception e)
                {
                    System.err.println(e);
                }
            }
        }


    This connects to the database on the same pc called test_database using the login name msc89nxv and password n0121429.

    Could someone tell me what I would need to do to connect to a database on a networked pc. How would I need to create a new user and grant privileges and how would the java code need to be altered.

    I really appreciate any help,

    Thanks

  2. #2
    Join Date
    Jan 2005
    Posts
    362

    Ip

    You have to know the IP address (or maybe the "name" of this PC) of the PC and to put this insteed of "localhost".
    You need a mysql user which can connect to mysql from the network.
    Dimis

  3. #3
    Join Date
    Mar 2006
    Posts
    41
    Quote Originally Posted by dimis2500
    You have to know the IP address (or maybe the "name" of this PC) of the PC and to put this insteed of "localhost".
    You need a mysql user which can connect to mysql from the network.
    Dimis
    Thats great thanks. I tried it out and it works fine.

    Cheers

Posting Permissions

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