Results 1 to 4 of 4
  1. #1
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default MySql , Eclipse . Type MisMatch of Connection object . Please help me...

    Please help me to fix this code:

    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();
      }
      }
    }
    Console Error:

    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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default 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:
    Java Code:
    ((java.sql.Connection) con).createStatement();
    is unecessary by the way.
    Just con.createStatement().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: MySql , Eclipse . Type MisMatch of Connection object . Please help me...

    Quote Originally Posted by Tolls View Post
    Do you have another class called Connection in your class path?
    No.
    But when i combined line-6 :
    Java Code:
    Connection con= null;
    and line-15
    Java Code:
    con = DriverManager.getConnection(url+db, user, pass);
    ,

    that error gone away (as below).
    Java Code:
    java.sql.Connection con = DriverManager.getConnection(url+db, user, pass);
    After above changed is made , i replaced
    Java Code:
    st = ((java.sql.Connection) con).createStatement();
    by
    Java Code:
    st.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?
    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 02-19-2011, 10:34 PM
  2. Object mismatch, not sure why
    By olddog in forum New To Java
    Replies: 1
    Last Post: 01-29-2011, 10:18 PM
  3. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  4. Replies: 6
    Last Post: 02-21-2010, 12:54 AM
  5. Replies: 2
    Last Post: 11-16-2007, 01:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •