How to connect mysql server with IP addres

Discussion in 'Java' started by umeshwar.yalla, Dec 13, 2011.

  1. umeshwar.yalla

    umeshwar.yalla New Member

    Joined:
    Dec 13, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am beginner to this field ,i need suggestions from you to complete my task.

    I want to connect to mysql db server through my localhost(PC) to remote server.

    The steps i have followed to connect db server.

    Step 1: I have installed jdk 1.6.29 version in my PC and also JDBC driver(mysql-connector-java-5.1.18) using windows XP3 pack

    Step 2: Set the path for JDK and classpath for JDBC driver.

    Step 3: The details available with me
    -> Server IP address
    -> Data base Name
    -> User name and password
    -> No port number available
    Step 4: import java.sql.*;
    Code:
        public class DB {
            // JDBC driver name and database URL
            static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
            static final String DB_URL = "jdbc:mysql://IP adress/Database name";
    
            //  Database credentials
            static final String USER = "Umeshwar";
            static final String PASS = "XXXXX";
       
            public static void main(String[] args) {
                Connection conn = null;
                Statement stmt = null;
                try{
                    //STEP 2: Register JDBC driver
                    Class.forName("com.mysql.jdbc.Driver").newInstance();;
        
                    //STEP 3: Open a connection
                    System.out.println("Connecting to database...");
                    conn = DriverManager.getConnection(DB_URL,USER,PASS);
    
                    //STEP 4: Execute a query
                    System.out.println("Creating statement...system is connected");
                    conn.close();
                
                }catch(SQLException se){
                    //Handle errors for JDBC
                    se.printStackTrace();
                }catch(Exception e){
                    //Handle errors for Class.forName
                    e.printStackTrace();
                }
                    System.out.println("Goodbye!");
            }//end main
        }//end FirstExample
    
    But finally when i execute on cmd it will display error i connection failure.

    Please tell me where i was wrong.
     
    Last edited by a moderator: Dec 13, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice