为什么我无法使用java连接到mysql数据库

时间:2015-03-13 16:35:13

标签: java mysql jdbc

我之前使用过jdbc驱动程序。但是对于这段程序我无法连接到db.This不会抛出任何异常或任何东西。只是不会连接。我也无法在网上找到解决方案。下面是我试图运行的代码:(请帮忙解决这个问题。提前谢谢你:)

public class HeapMySql<T extends Comparable<T>> implements HeapInterface {

static final String DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://localhost/Heap"; 

static final String USERNAME = "root";
static final String PASSWORD = "";

private int size = 0 ;
String sql;
static Statement stmt = null;
static Connection conn = null;
static ResultSet rs = null;

public void HeapMySql(){

    try
    {       
        sql = "CREATE TABLE testHeap (index integer, value integer);";  
        stmt.executeUpdate(sql);
        System.out.println("Done");
    }catch(Exception e){

    }        

}

public static void main(String [] arg){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Connecting to a selected database...");
        conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
        System.out.println("Connected database successfully...");

        System.out.println("Creating table in given database..."); //lets create a table in our database
        stmt = conn.createStatement();

        HeapMySql test1 = new HeapMySql<>();

    }catch(ClassNotFoundException | SQLException ex){

    }finally{

    }       

1 个答案:

答案 0 :(得分:1)

构造函数没有返回类型docs

void移除public void HeapMySql(),它将完成工作。 同样如评论中所述,您应该在stacktrace块中打印catch。这样可以很容易地理解异常并解决问题。