This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Syntax of getConnection() method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Syntax of getConnection() method" Watch "Syntax of getConnection() method" New topic
Author

Syntax of getConnection() method

Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Hi,
I am confused about the syntax of getConnection() method. Can anyone tell me what's wrong with the following syntax? My databasename, username and password is "CC". And my database is on the local machine.


con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433/CC","CC","CC");

Thanks in advance
JEAN
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

the method usage seesm to be correct.
can you please post the exeception that program raise ? by using the exception users will be able to help you about your problem.


Masoud Kalali
Software Engineer - My Weblog - GlassFish Security
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Hi,
The following is the exception.
----------------------------------------------
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unable to conn
ect. Invalid URL.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source
)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at UBDAO.main(UBDAO.java:41)
Error Trace in getConnection() : [Microsoft][SQLServer 2000 Driver for JDBC]Unab
le to connect. Invalid URL.
------------------------------------------------------------------------

The problem is with the syntax where I am giving the database name. There's no problem with the authentication to the database since the same database, username and password is working fine if I pass the paramenter as a string to this getConnection() method as follows.
---------------------------------------------
import java.io.*;
import java.sql.*;

public class UBDAO {
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "CC";
private final String userName = "CC";
private final String password = "CC";


private String getConnectionUrl()
{
return url+serverName+":"+portNumber+";databaseName="+databaseName+";";
}

public static void main(String args[])
{
UBDAO ub = new UBDAO();
Connection con = null;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection(ub.getConnectionUrl(),ub.userName,ub.password);
if(con!=null) System.out.println("Connection Successful!");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
}
}
-----------------------------------------------------------
Thanks
JEAN
[ May 22, 2006: Message edited by: Jean Fore ]
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

microsoft provided two jdbc driver for its SQL server
in one of them the connection url , as they said in its documentation should look like :




and in the other one which is older the syntax is like :



it is possible that MS JDBC driver connection string does not support protocol:vendorspecefic:://server?/DatabaseNAme model .
Vishwanath Rajashekar
Greenhorn

Joined: May 23, 2006
Posts: 2
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc dbc SN","un","pwd");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from table_name");

DSN-->DSN name you have used while creating DSN as mentioned below.
un-->user name for the sql server.
pwd-->password for the sql server.

You might have to create a Data Source Name(DSN) by going to ControlPanel-->Administrative Tools-->DataSources(ODBC) and click add to create the DSN and follow on.Hope this might help you.
[ May 23, 2006: Message edited by: Vishwanath Rajashekar ]
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Originally posted by Vishwanath Rajashekar:
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


If you're using the native type IV drivers for SQL Server, as the original poster is trying to do, then you most certainly don't want to use a DSN and the JDBC-ODBC bridge. For many purposes, the briddge has some insurmountable problems, not least of which is the lack of support for concurrent usage in multi-threaded programs.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Syntax of getConnection() method
 
Similar Threads
JSP mySQL exception
DataAccessRemote Beta 1.4
Issue in connecting to database named "test;abc" (semi colon).
Derby (Cloudscape): how to query 2 or more tables in one query-statement?
Please help !!!!!!!!!!!