• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Syntax of getConnection() method

 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic