为什么我无法使用 java 连接到 mysql 数据库
Why i can not connect to mysql db using java
我已经为这段程序使用了 jdbc 驱动程序 before.But 我无法连接到 db.This 没有抛出任何异常或任何东西。只是不会连接。我在网上找不到解决方案 either.Below 是我尝试的代码 运行 :( 请帮助解决这个问题。提前谢谢你:)
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{
}
构造函数没有 return 类型: docs
从 public void HeapMySql()
中删除 void
,它将完成工作。
同样如评论中所述,您应该在 catch
块中打印 stacktrace
。这使得理解异常和解决问题变得容易。
我已经为这段程序使用了 jdbc 驱动程序 before.But 我无法连接到 db.This 没有抛出任何异常或任何东西。只是不会连接。我在网上找不到解决方案 either.Below 是我尝试的代码 运行 :( 请帮助解决这个问题。提前谢谢你:)
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{
}
构造函数没有 return 类型: docs
从 public void HeapMySql()
中删除 void
,它将完成工作。
同样如评论中所述,您应该在 catch
块中打印 stacktrace
。这使得理解异常和解决问题变得容易。