• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Syntax of getConnection() method

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 531
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 531
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic