Results 1 to 2 of 2
  1. #1
    shorawitz is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default JDBC not connecting from Thread Class

    I'm trying to connect to a MySQL DB using the JDBC driver. The same code works from the "main" method, but when I try to use it in a thread class, it first complains that the type is wrong for my getConnection assignment and if I cast the (Connection) type as in the code below, the app just terminates at that point without error.

    Is there a different way to query a DB from a thread than a normal method? I've tried it from "run", separate methods, separate class. No luck.

    Java Code:
    try {
        System.out.println("try: registerDriver");
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());  // Register the JDBC MySQL driver
        System.out.println("try: getConnection");
        try {
            conn = (Connection) DriverManager.getConnection(dbUrl+dbName,dbUser,dbPass);
    
            // IT NEVER MAKES IT THIS FAR, EXITS - NO ERROR
            System.out.println("try: passing 'query'");
            // other code
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
    } catch  (Exception excp) {
        excp.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                System.out.println("Closing Db connection");
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    i just tried to connect to my mysql inside a thread and i had no problem (replacing dburl, user and password) using the following code:

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    public class DbConnection extends Thread {
    
    	public void run() {
    
    		Connection connection = null;
    
    		try {
    			System.out.println("try: registerDriver");
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			System.out.println("Couldn't find the driver!");
    			e.printStackTrace();
    			System.exit(1);
    		}
    
    		try {
    			System.out.println("try: getConnection");
    			connection = DriverManager.getConnection(
    					"dburl", "user", "password");
    		} catch (Throwable e) {
    			System.out.println("Couldn't get connection to localhost!");
    			e.printStackTrace();
    			System.exit(1);
    		}
    	}
    }
    and

    Java Code:
    public class TestDbConnection {
    
    	public static void main(String[] args) {
    		DbConnection dbcon = new DbConnection();
    		dbcon.start();
    	}
    }

Similar Threads

  1. interrupt a jdbc connection thread
    By ko_aung in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-12-2010, 08:04 PM
  2. JDBC class check
    By DavidBainbridge in forum New To Java
    Replies: 4
    Last Post: 08-17-2009, 12:52 PM
  3. JDBC commands for connecting to sqlserver
    By sudukrish in forum Advanced Java
    Replies: 7
    Last Post: 04-22-2009, 09:48 AM
  4. JSP Connecting to MS SQL SERVER 2000 using JDBC
    By hisouka in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 09-02-2008, 09:23 AM

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
  •