如何连接退出数据库

时间:2016-12-31 03:46:53

标签: java sqlite database-connection

起初我使用Firefox SQLite Manger扩展创建了一个名为“DataBase”的数据库。现在,我想通过java程序连接到该数据库,以便遍历整个数据库。 为了连接数据库,我在互联网上找到了这样的方法。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectJDBC3 {
   public static void main(String[] args) {
    String databaseURL = "jdbc:mysql://localhost:3306/DataBase";  //just change the databasename.
    String user = "Newaj_69"; //Here i have given only the user name of the pc
    String password = ""; //As i have no password in the pc
    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(databaseURL, user, password);
        if (conn != null) {
            System.out.println("Connected to the database");
        }
    } catch (ClassNotFoundException ex) {
        System.out.println("Could not find database driver class");
        ex.printStackTrace();
    } catch (SQLException ex) {
        System.out.println("An error occurred. Maybe user/password is invalid");
        ex.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
  }}

但仍然无法访问数据库。现在我想知道我的进程是否正确?如果没有,对任何建议都会非常有帮助。

0 个答案:

没有答案
相关问题