Type mismatch: cannot convert from Connection to C

2019-07-07 15:12发布

问题:

I want JDBC connection to MSaccess. But

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:access");

it gives

Type mismatch: cannot convert from Connection to Connection

what is solution for that?

I m using Servlet and jsp in jsp -

Organization Name:    <input type="text"  name="Organization_name" ><br>

i want when Organization_name entered it will be add it in my access database i have tried but i m facing following problem

Connection con = DriverManager.getConnection("jdbc:odbc:access");

it gives Type mismatch: cannot convert from Connection to Connection

回答1:

you need Connection from java.sql it seems you have imported a wrong class

and getConnection() needs complete jdbc URL.

In very simple words your code should have following imports

import java.sql.Connection



回答2:

try this:

import java.sql.Connection;
import java.sql.DriverManager; 

...

try { 
   String username = "";
   String password = "";
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   DriverManager.getConnection("jdbc:odbc:northwind", username, password);
   ...

northwind is the name of the sample database in Access. Use whatever you've got.



回答3:

The Connection object that is being returned by getConnection() is not the same Connection class you have referenced in your package imports at the top of your class file.



回答4:

This could be an classloader issue. The object created is from the different classloader and is referred in another classloader.



标签: java jdbc