"Data source name not found and no default driver"
java to mysql connection code:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
con = DriverManager.getConnection(url, "root", "sai");
stmt = con.createStatement();
System.out.println("Connection established");
after excution of ab开发者_运维技巧ove code
"Driver loaded" message printed on console but,
after that following exception is printed -
[Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified
i have already create DSN(MYSQL ODBC 3.51 Driver) for project
Please tell me solution of above problem.
"If somehow possible, don't use a JdbcOdbc driver, but a pure JDBC-Driver for your database!"
An example MySql pure JDBC-Driver would be the com.mysql.jdbc.Driver class found in the mysql-connector-java-5.x.x.jar file available for download from MySQL.
A good explanation of the differences between JDBC and JDBC-ODBC are available on wikipedia
If somehow possible, don't use a JdbcOdbc driver, but a pure JDBC-Driver for your database!
For the question of how to register the driver, from the javadocs for java.sql, Interface Driver:
It is strongly recommended that each Driver class should be small and standalone so that the Driver class can be loaded and queried without bringing in vast quantities of supporting code.
When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by calling
Class.forName("foo.bah.Driver")
A separate call to 'newInstance' or registerDriver' is Cargo-Cult-programming.
It's a while since I've done this, but you could try to explicitly register your driver with the DriverManager class. This means that you're not depending upon something unseen implicitly happening in the background.
DriverManager.registerDriver((Driver)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance());
con = DriverManager.getConnection(url, "root", "sai");
This will ensure that the "sun.jdbc.odbc.JdbcOdbcDriver" class is loaded and connected to the DriverManager.
精彩评论