Results 1 to 13 of 13
Thread: new to JDBC
- 05-01-2008, 07:40 AM #1
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
- 05-01-2008, 08:54 AM #2
Hello,
what have you done and what errors are your getting.
Try this code.
Java 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,संजीव
- 05-01-2008, 09:14 AM #3
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
"java.lang.ClassNotFoundException" thats the error im getting..it something to do with da sql driver i guess.
- 05-01-2008, 10:18 AM #4
Yes your right this error is because the SQL JDBC driver cannot be found."java.lang.ClassNotFoundException" thats the error im getting..it something to do with da sql driver i guess.
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.
:DDid this post help you? Please
me! :cool:
- 05-01-2008, 10:40 AM #5
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
i download the driver and then set the classpath..stil im getting the same error :(
- 05-01-2008, 10:48 AM #6
What JDK are you using? What editor...?
Use this code. Create a class called connectDS
Java 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); } } }Last edited by DonCash; 05-01-2008 at 11:05 AM.
Did this post help you? Please
me! :cool:
- 05-01-2008, 10:52 AM #7
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
im using eclipse..
btw da code u gave me had error
"com.microsoft cannot be resolved to a type"
- 05-01-2008, 11:04 AM #8
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.Last edited by DonCash; 05-01-2008 at 11:09 AM.
Did this post help you? Please
me! :cool:
- 05-01-2008, 11:12 AM #9
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
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"
- 05-01-2008, 11:17 AM #10
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
me! :cool:
- 05-01-2008, 11:27 AM #11
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
got an error
"con cannot be resolved"
- 05-01-2008, 11:29 AM #12
Member
- Join Date
- May 2008
- Posts
- 14
- Rep Power
- 0
sorry the error was
con = ds.getConnection();
"Type mismatch: cannot convert from Connection to Connection"
- 05-01-2008, 11:34 AM #13
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:
Java 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
me! :cool:
Similar Threads
-
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
Jdbc
By siwa in forum JDBCReplies: 4Last Post: 11-09-2007, 07:31 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM -
Ha-jdbc 2.0
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-17-2007, 05:40 PM -
Help with JDBC
By Eric in forum JDBCReplies: 2Last Post: 06-28-2007, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks