Results 1 to 4 of 4
  1. #1
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default Problem with database connection

    This is my code:
    Java Code:
    import com.mysql.jdbc.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Date;
    
    public class Try {
    
           public static void main (String[] args)
           {
               Connection conn = null;
    
               try
               {
                   String userName = "root";
                   String password = "test";
                   String url = "jdbc:mysql://localhost:3307/Books";
                   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                   conn = DriverManager.getConnection (url, userName, password);
                   System.out.println ("Database connection established");
                   
               }
               catch (Exception e)
               {
                   System.err.println ("Cannot connect to database server");
                   
               }
                       Statement s = conn.createStatement ();
    
    
           }
    
    }
    The database can, and is connecting to a MySql database on my machine. The problem is, the Statement command wont work.
    Java Code:
     Statement s = conn.createStatement ();
    Gives the error: Incompatible types. Found: java.sql.Statement. Required: com.mysql.jdbc.Statement

    Any ideas? I think it has something to do with the jdbc being used. This whole database thing is driving me insane, i've spent far too many hours working on this.

  2. #2
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    heh nevermind, I solved the problem.

    This always happens to me when programming. I take hours to try and solve a problem and it's usually staring me right in the face. Just by writing this out for you to see and actually thinking about it logically I worked it out. Basically, I had imported the wrong library; I had imported "import com.mysql.jdbc.Statement" when I should have imported "import java.sql.Connection" heh.

  3. #3
    digioleg is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Please, do you know how to close connection? I have separate method to close it with Oracle but do not know how to close

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

Similar Threads

  1. [SOLVED] database connection
    By casid in forum New To Java
    Replies: 8
    Last Post: 06-05-2009, 12:42 PM
  2. database connection in jsf
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-06-2008, 06:53 AM
  3. Database Connection
    By CompleteBeginner in forum New To Java
    Replies: 2
    Last Post: 05-24-2008, 02:30 PM
  4. Database Connection
    By vipinkumarsolanki in forum Advanced Java
    Replies: 2
    Last Post: 11-26-2007, 06:36 AM
  5. Database connection
    By oaklander in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 11:11 PM

Posting Permissions

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