Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC retreiving Datas and saving Datas via Database SQL

 
Agunbiade Ademola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have try everything to retrieve data using JDBC from SQL data base, i have downloaded the JDBC Driver and i install it properly , but the initialization seem to be the problem because I'M using window authentication to sign in my SQL server and every tutorial i watch use Password and Username, when ever i call my application this way it return ClassNotFoundExceptional error:

import java.sql.*;
//import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement;

public class SimpleJDBC
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://ADEBIADE:1433");

String connectionUrl = "jdbc:sqlserver://localhost:1433;"+"database=TimeCards;integratedSecurity=true;";
Connection conn = DriverManager.getConnection(connectionUrl);

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select distinct FirstName from TimeCards.dbo.Employee(HumanResources)");

while(rs.next())
{
System.out.println(rs.getString(1) );
}

st.close();



//System.out.println(conn.getMetaData().getDatabaseProductName);
conn.close();
}
catch(ClassNotFoundException xp)
{
System.out.println("The ClassNotFoundException exception has Occured"+xp);
}
catch(SQLException xcp)
{
System.out.println("The SQLException exception has occured:"+xcp);
}

}
}



 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please print the error trace? That way people will know what class is not being found.
 
Agunbiade Ademola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It actaully print out ClassNotFoundException exception has Occuredjava.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver,

Although my JDBC Driver was instal to my c:/ drive, may be it suppose to be in Program Files
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that JDBC driver in your classpath?
 
Agunbiade Ademola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for following my respond i copy it to the path because i don't have classpath variable and this is how it was C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x64; Im wondering is because of i don't use password and username authentication
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agunbiade Ademola wrote:Thank you for following my respond i copy it to the path because i don't have classpath variable and this is how it was C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x64; Im wondering is because of i don't use password and username authentication

No, it's because the file isn't in your classpath. Path is not equivalent.

How are you running your program? If command line, you can pass -classpath at the command line. If using a server, it depends on the server.
 
Agunbiade Ademola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done has you said now, the exceptional error is no more prompting but is throwing an error of:

Could not find or load main class SimpleJDBC.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is you changed java SimpleJDBC to java -cp pathToJdbc SimpleJDBC.

Instead, include the current path as well:

On Windows: java -cp ".;pathToJdbc" SimpleJDBC.
On UNIX: java -cp ".:pathToJdbc" SimpleJDBC.
 
Agunbiade Ademola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You! but i don't understand exactly what you mean, I think is this line of code that cause it: String connectionUrl = "jdbc:sqlserver://localhost:ADEBIADE;"+"database=TimeCards;integratedSecurity=true;"; You see that ADEBIADE, is the SQL server name that I'm using. I really mean to know this JDBC.

The point is: My SQL server is 2008 ok, and my JDBC is install in this direction package C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0, while the SmpleJDBC.java is in the folder called Java in my C:/ directory(C:\Java) and I create a JDBC folder in the package of Java Folder and the SimpleJDBC.java is embeded in JDBC Folder like this (C:\Java\JDBC\SimpleJDBC.java) and I'm using Text Pad AS IDE while I'm using CMD to run it.

in my PATH drive is save the JDBC driver
CLASSPATH i save JDBC driver again. so what is happening? I have Tomcat, if Tomcat will do it better explain how i can use tomcat rather that that manual way.
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion would fit better in our databases forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic