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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need help" Watch "Need help" New topic
Author

Need help

Rakesh Khire
Greenhorn

Joined: Oct 17, 2011
Posts: 2
Hi,
I am new to this Java and trying to write following program

import java.sql.*;
public class JDBCDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc: odbc:StudTableDSN");
Statement stmt=conn.createStatement();
String query="select * from StudTable where rollno=2";
ResultSet rs=stmt.executeQuery(query);
rs.next();
System.out.println("Roll No:"+rs.getInt(1));
System.out.println("Name:"+rs.getString(2));
System.out.println("Average:"+rs.getDouble(3));
conn.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}


and getting the error


C:\Program Files\Java\jdk1.6.0_23\bin>javac JDBCDemo.java

C:\Program Files\Java\jdk1.6.0_23\bin>java JDBCDemo
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not fou
nd and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3
23)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at JDBCDemo.main(JDBCDemo.java:9)

C:\Program Files\Java\jdk1.6.0_23\bin>


friends please help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch

Don’t put your own code in the “Program Files” folder. Have a look at this post for a better suggestion.
Where have you put the .jar which contains the connectors? How are you adding it to your classpath?
Rakesh Khire
Greenhorn

Joined: Oct 17, 2011
Posts: 2
thank you for your welcome Campbell Ritchie, I done as you suggested that i moved my code but still the same problem is there.


F:\Ad_Java\Exp10>javac JDBCDemo.java

F:\Ad_Java\Exp10>java JDBCDemo
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not fou
nd and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3
23)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at JDBCDemo.main(JDBCDemo.java:9)

F:\Ad_Java\Exp10>





and can you please help me little more and tell me that where can i find .jar??

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
So where did you put the .jar? You will have to search for the location of the ODBC driver. Try Googling, and you will find lots of places. This page, which doesn’t seem to work for Java6 and Java7, has a link to a database of drivers. I might be wrong about the numbering; those numbers might refer to EE numbers rather than SE and EE5 may be the most recent.

Go through the database; if for example you go to M for MS SQL Server, you can find about 50 different drivers in the database. Decide which is most suitable and download it. See whether this Java tutorials page helps you get a connection.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you look here, it would appear the most recent version of JavaEE is 6.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help