Results 1 to 4 of 4
- 03-25-2012, 08:17 PM #1
Member
- Join Date
- Jan 2012
- Location
- India
- Posts
- 32
- Rep Power
- 0
MySql , Eclipse . Type MisMatch of Connection object . Please help me...
Please help me to fix this code:
Console Error:Java Code:import java.sql.*; public class ColumnName{ public static void main(String[] args) { System.out.println("Getting Column Names Example!"); Connection con= null; Statement st = null; String url = "jdbc:mysql://localhost:3306/"; String db = "my_sample_db"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = ""; try{ Class.forName(driver); con = DriverManager.getConnection(url+db, user, pass); try{ st = ((java.sql.Connection) con).createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM employee"); ResultSetMetaData md = rs.getMetaData(); int col = md.getColumnCount(); System.out.println("Number of Column : " + col); System.out.println("Columns Name: "); for (int i = 1; i <= col; i++){ String col_name = md.getColumnName(i); System.out.println(col_name); } } catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } catch (Exception e){ e.printStackTrace(); } } }
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from java.sql.Connection to Connection
at ColumnName.main(ColumnName.java:15)
As a newbie , i started using database from few days ago. So , I think i did not configure mysql-connector/j properly. So, please give me some tips about this. Advance Thankssssss.
- 03-26-2012, 10:44 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,455
- Rep Power
- 16
Re: MySql , Eclipse . Type MisMatch of Connection object . Please help me...
Do you have another class called Connection in your class path?
That's what that error looks like.
This cast:
is unecessary by the way.Java Code:((java.sql.Connection) con).createStatement();
Just con.createStatement().Please do not ask for code as refusal often offends.
- 03-26-2012, 11:41 AM #3
Member
- Join Date
- Jan 2012
- Location
- India
- Posts
- 32
- Rep Power
- 0
Re: MySql , Eclipse . Type MisMatch of Connection object . Please help me...
No.
But when i combined line-6 :and line-15Java Code:Connection con= null;
,Java Code:con = DriverManager.getConnection(url+db, user, pass);
that error gone away (as below).
After above changed is made , i replacedJava Code:java.sql.Connection con = DriverManager.getConnection(url+db, user, pass);
byJava Code:st = ((java.sql.Connection) con).createStatement();
At first time when this casting was not done, an error was thrown . But now no cast is done , and there is no error ?! How?Java Code:st.createStatement();
But still there are some questions : 1) Wont eclipse recognise our code and the jar files added under 'referenced libraries' itself when when compilation and execution?
2) When creating objects of classes of jdbc , is it necessary to write code like : java.sql.Connection..... etc... ?Last edited by j_arif123; 03-26-2012 at 11:58 AM.
- 03-26-2012, 12:34 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,455
- Rep Power
- 16
Re: MySql , Eclipse . Type MisMatch of Connection object . Please help me...
There must have been another Connection class somewhere for that error.
Anyway, normally you don't have to specifiy the full classname (class and package).
The only time is if there is a clash of names, ie two classes with the same name from different packages (eg java.util.Date and java.sql.Date).
So, since you are including java.sql.Connection (it's generally better to include each class, rather than using '*'), you should not need to say Java.sql.Connection.Please do not ask for code as refusal often offends.
Similar Threads
-
create a poolable connection using mysql eclipse helios and apache tomcat 6 with java
By computerbum in forum EclipseReplies: 1Last Post: 02-19-2011, 10:34 PM -
Object mismatch, not sure why
By olddog in forum New To JavaReplies: 1Last Post: 01-29-2011, 10:18 PM -
Type Mismatch error
By and0rsk in forum New To JavaReplies: 2Last Post: 10-10-2010, 11:16 AM -
SQLException:java.sql.Exception:Data type mismatch in criteria expresion
By Dumisan in forum JDBCReplies: 6Last Post: 02-21-2010, 12:54 AM -
type mismatch: cannot convert from double to float
By bugger in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks