aspose file tools*
The moose likes JDBC and the fly likes basic SQL  connection steps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "basic SQL  connection steps" Watch "basic SQL  connection steps" New topic
Author

basic SQL connection steps

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Hello, i don't know if this topic exists already or not, but i'll try my luck anyways. I am currently trying to make a connection with an sql server, and i have absolutely no idea what to do. I work with SQL Express 2008 R2 version. I downloaded the sqljdbc.jar and sqljdbc4.jar drivers from the internet. I put them in the folder where my project is, and added them as external jars to my project (in eclipse: project>properties>Java Build Path>add external jars) . Then i tried to run the following code:

import java.sql.*;
import java.util.*;

class JdbcTest1 {

public static void main(String args[]) {
try { // Step 1: Load the JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Step 2: Establish the connection to the database.
String url = "jdbc:microsoft:sqlserver://localhost:1433/E"; //the port i use is 1433 and E is my database
Connection conn = DriverManager.getConnection(url,"sa","blabla"); //sa is the username and blabla is the password
} catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}

}

And i get the error: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433/E
And from now on i'm stuck. Been on a ton of forums and blogs and so on, tried a lot of solutions, none worked. Can someone please help me and tell me step-by-step what i have to do?
PS: I have already dealt with TCP/IP and Authentication Method problems, so I guess that everything comes from the code and the drivers
Thank you very much
Santhosh ayiappan
Ranch Hand

Joined: Jan 30, 2007
Posts: 80

Do you have the driver Jar file in the claspath ?

~Santhosh
vivek kumar jaiswal
Greenhorn

Joined: Jul 31, 2008
Posts: 24

Hi Rus ,


Jar is not supported your current version sql express 2008 R2. Find right jar
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Yes I do. I'm thinking it's the code i put as the url or something. I don't know. And i'm using the sqldbc4.jar driver, since i've read that that is the proper driver for sql 2008
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Nevermind, I found the problem
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Rus Corina wrote:Nevermind, I found the problem

What was it?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rohit kumar singh
Greenhorn

Joined: Jul 08, 2011
Posts: 2

i m also trying to connect ms sql 7 with servlet and i m always getting the errors....i do not know what type of jar files i needed and where i will get it from.....so please tell me how i will solve the problem if you already solve the problem......Reply me as soon as possible
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Jeanne Boyarsky: the problem was that i did not use the proper driver name when i was trying to load it, and i was using the wrong url when trying to establish the connection with the server. Oh, and at the beginning i used a wrong driver. I used sqljdbc.jar instead of sqljdbc4.jar


Rohit kumar singh : I don't really know what servlet is, but i used the sqljdbc4.jar driver for the connection. You can get it here: http://www.microsoft.com/download/en/details.aspx?id=2505
Sithi Kone
Greenhorn

Joined: Aug 13, 2011
Posts: 1
Hi folks,
I'm very new to both Java and SQL and have just joined this forum (my very first forum membership ever) after having racked my brains out, searching and reading quite a bit of forums and blogs on how to connect to MS SQL Server 2008 R2 64 bit. I'm trying Rus' script as it seems fairly straightforward.
-I've done all the prep work with the jar files, both sqljdbc4.jar and sqljdbc.jar in Eclipse and made sure the port 1433 is in place.

import java.sql.*;

class conn2 {

public static void main(String args[]) {
try { // Step 1: Load the JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Step 2: Establish the connection to the database.
String url = "jdbc:microsoft:sqlserver://localhost:1433/JAVADB"; //the port i use is 1433 and JAVADB is my database
Connection conn = DriverManager.getConnection(url,"SS",""); // I'm not using a password, would this be a problem??
} catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}


result I'm getting:
"Got an exception!
No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433/JAVADB"

-I noticed that my server name is "SS-THINK\SQLEXPRESS" but I can't use this name b/c it contains a single "\" which isn't accepted in Java language
-I've also set the authentication to using both SQL Server and Windows authentication

Thanks again all- I would truly appreciate any help out.

Sithi

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic SQL connection steps
 
Similar Threads
problem in connecting sql 2005
JDBC with MS SQL 2000
Finding out the correct URL
trying to connect sql server 2000 - not connected
OC4J deploment problem Leaked Connection Related