Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-01-2008, 08:40 AM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
new to JDBC
im new to Java and i want conenct a sql server 2005 database to a java application..can i knw da procedure and whr to download sqlserver driver.and how to set the classpath and a simple sample code to connect to a database.thank u in adavance

Hussain
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-01-2008, 09:54 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Hello,

what have you done and what errors are your getting.


Try this code.

Code:
try{ Class.forName(driver); conn = DriverManager.getConnection(url,uid,pass); stmt = conn.createStatement(); } catch(ClassNotFoundException ce) { System.out.println("ERROR:"+ce.getMessage()); } catch(SQLException se) { System.out.println("ERROR:"+se.getMessage()); } }
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-01-2008, 10:14 AM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
"java.lang.ClassNotFoundException" thats the error im getting..it something to do with da sql driver i guess.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-01-2008, 11:18 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Quote:
"java.lang.ClassNotFoundException" thats the error im getting..it something to do with da sql driver i guess.
Yes your right this error is because the SQL JDBC driver cannot be found.

You can download the driver here:

Microsoft SQL Server 2005 JDBC Driver

Do a search on these forums before you post again because you'll find these questions have been asked and answered 100 times before.

__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-01-2008, 11:40 AM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
i download the driver and then set the classpath..stil im getting the same error
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-01-2008, 11:48 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
What JDK are you using? What editor...?

Use this code. Create a class called connectDS

Code:
import java.sql.*; import com.microsoft.sqlserver.jdbc.*; public class connectDS { public static void main(String[] args) { // Declare the JDBC objects. Connection con = null; try { // Establish the connection. SQLServerDataSource ds = new SQLServerDataSource(); ds.setUser("UserName"); ds.setPassword("*****"); ds.setServerName("localhost"); ds.setPortNumber(1433); ds.setDatabaseName("AdventureWorks"); con = ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) try { con.close(); } catch(Exception e) {} System.exit(1); } } }
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 05-01-2008 at 12:05 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-01-2008, 11:52 AM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
im using eclipse..

btw da code u gave me had error
"com.microsoft cannot be resolved to a type"
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-01-2008, 12:04 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Have you imported the JDBC jar into your Eclipse project?

Try the new code example above. Sorry the original one I did was from memory. This new one is taken from the SQLServer JDBC help and I cut it down a bit.
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 05-01-2008 at 12:09 PM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-01-2008, 12:12 PM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
i extracted my SqlServer2005 dirver at C:\Program Files\
and then set the class path.thats wht i did..i didnt import a jdbc jar file.how to import the JDBC jar???

got an error from the code u gave
"The import com.microsoft cannot be resolved"
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-01-2008, 12:17 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
OK this is because you haven't imported the JDBC driver into Eclipse.

You need to locate the .jar file that is extracted once you download the exe from that Microsoft Download site.

Go to Eclipse: In the package navigation window on the left hand side right click your current Project.

Click 'Properties' then on the left hand side click 'Java Build Path' then click 'Add External JARs..' on the right.

Navigate to the JDBC .jar file and double click it. This will import the jar into your project.

Click OK.

Now try to run your code...
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-01-2008, 12:27 PM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
got an error

"con cannot be resolved"
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-01-2008, 12:29 PM
Member
 
Join Date: May 2008
Posts: 14
hussainzim is on a distinguished road
sorry the error was
con = ds.getConnection();
"Type mismatch: cannot convert from Connection to Connection"
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-01-2008, 12:34 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Hey hussainzim, you do have the ability to edit your previous posts so it would be best to edit rather than post a new one.

I can't actually test this code because I dont have a SQL database to connect to.

All I can suggest is looking at the Help that comes with the JDBC and try some of their examples.

Try:

Code:
import java.sql.*; public class connectURL { public static void main(String[] args) { // Create a variable for the connection string. String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;user=UserName;password=*****"; // Declare the JDBC objects. Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Establish the connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(connectionUrl); // Create and execute an SQL statement that returns some data. String SQL = "SELECT TOP 10 * FROM Person.Contact"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); // Iterate through the data in the result set and display it. while (rs.next()) { System.out.println(rs.getString(4) + " " + rs.getString(6)); } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch(Exception e) {} if (stmt != null) try { stmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} } } }
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use JDBC Template classes to control basic JDBC processing and error handling Java Tip Java Tips 0 04-01-2008 11:17 AM
Jdbc siwa Database 4 11-09-2007 08:31 AM
How to use JDBC Template classes to control basic JDBC processing and error handling JavaBean Java Tips 0 09-28-2007 01:56 PM
Ha-jdbc 2.0 JavaBean Java Announcements 0 07-17-2007 06:40 PM
Help with JDBC Eric Database 2 06-28-2007 07:00 PM


All times are GMT +3. The time now is 08:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org