Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to connect to a remote database?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How to connect to a remote database?

    There are two things that I am having trouble understanding:
    1. How to connect to the server. I do not know if I should use localhost or not? (I am linking to a database that is not on my local machine, if that matters)
    2. How to connect to the correct database, as there are a couple of databases under that server

    This is what I have so far:
    MAIN -> My Server
    1433: TCP/IP Port
    DB_Accounts -> The Database I want to connect to

    String dbURL = "jdbc:sqlserver://MAIN\\SQLEXPRESS:1433/DB_Accounts;user=myuser;password=mypass;"; // Connect the server and the database

    thanks for any help given


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: How to connect to a remote database?

    Quote Originally Posted by ace84 View Post
    There are two things that I am having trouble understanding:
    1. How to connect to the server. I do not know if I should use localhost or not? (I am linking to a database that is not on my local machine, if that matters)
    2. How to connect to the correct database, as there are a couple of databases under that server

    This is what I have so far:
    MAIN -> My Server
    1433: TCP/IP Port
    DB_Accounts -> The Database I want to connect to

    String dbURL = "jdbc:sqlserver://MAIN\\SQLEXPRESS:1433/DB_Accounts;user=myuser;password=mypass;"; // Connect the server and the database

    thanks for any help given

    Hi,

    As you mentioned, if the DB exists in the remote server, You need to get the Correct IP where the correct DB exists and then U need to connect like the code below.
    Connection con = null;
     
    		try {			   			
    			String URL = "jdbc:oracle:thin:@121.340.19.249:1521";     			
    			String dataBaseName = "DBName";           
    			String userName = "username";         
    			String password = "password";    
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    			con = DriverManager.getConnection(
    					URL+":"+dataBaseName, userName,
    					password);
    		} catch (Exception e) {     
    			e.printStackTrace();   
    		}
    		return con;
    Try this.

    Regards,

    Janarthanan.K

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to connect to a remote database?

    Hey thanks for your help,

    I tried the code that you gave and I it didn't work because I am trying to connect to sql server database rather than a oracle db.

  4. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: How to connect to a remote database?

    oracle.jdbc.driver.OracleDriver
    you had tried after modifying driver for sql server and url name.

    Click Here

    Microsoft help
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Location
    Kochi,India
    Posts
    18
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to connect to a remote database?

    Quote Originally Posted by ace84 View Post
    Hey thanks for your help,

    I tried the code that you gave and I it didn't work because I am trying to connect to sql server database rather than a oracle db.
    The code posted by janarthanan seem to be fine. If you are using MySQL then change the Class.forName to the correct driver. Your IDE can point to the correct driver (Netbeans/Myeclipse etc.)

Similar Threads

  1. Java Code to connect remote server
    By idealguy in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 3rd, 2012, 05:59 AM
  2. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 05:45 PM
  3. connect java to a database
    By ridg18 in forum JDBC & Databases
    Replies: 1
    Last Post: December 25th, 2010, 11:28 AM
  4. Need urgent help to connect to Database
    By The Vicky in forum JDBC & Databases
    Replies: 3
    Last Post: November 27th, 2010, 11:09 AM
  5. Java Swing Tables ( JTable Models ) to connect to Database
    By javaprogrammer in forum JDBC & Databases
    Replies: 0
    Last Post: January 26th, 2010, 04:13 PM