Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   How to connect mysql server with IP addres (http://www.go4expert.com/forums/connect-mysql-server-ip-addres-t27354/)

umeshwar.yalla 13Dec2011 19:01

How to connect mysql server with IP addres
 
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.


All times are GMT +5.5. The time now is 08:09.