0

你好我有这个代码:

DatabaseManager.init("com.inet.tds.TdsDriver", "jdbc:inetdae:127.0.0.1?database=main", "username", "password", 10);

链接到:

public static void init(String JDBCDriver, String parURL, String parUserName, String parPassWord, int maxConnections) throws ClassNotFoundException, SQLException {

    Class.forName(JDBCDriver);
    URL = parURL;
    userName = parUserName;
    passWord = parPassWord;
    openConnection();
}

但是给了我这个错误:

DatabaseManager.getConnection() : java.sql.SQLException: url 不能为空

通过 DatabaseManager.class 中的这个脚本:

import java.sql.*;

public class DatabaseManager {

    private static String URL;
    private static String userName;
    private static String passWord;
    private static Connection connection = null;
public DatabaseManager() {
}
public static void executeSQL(String query, int maxtries) {

    if(maxtries == 0) {
        System.out.println("SQL query failed!");
        System.out.println("Query: "+query);
        return;
    }

    try{
        Connection connection = DatabaseManager.getConnection();
        Statement sm = connection.createStatement();
        sm.execute(query);
        return;
    }
    catch(Exception e) {

        System.out.println("Exception in DatabaseManager.executeSQL: " + e);

        System.out.println("SQL connection lost! Tries left:"+(maxtries-1));
        executeSQL(query, maxtries-1);
        return;
    }

}
public static void flush() {

    userName = null;
    passWord = null;
    if (connection != null) {
        URL = null;
        try {
            connection.close();
        } catch (SQLException sqle) {
            //  a database error occured    
            System.out.println("DatabaseManager.flush() : " + sqle);    
        }
    }
    connection = null;
}
public static Connection getConnection() {

    try {
        if (connection!=null) {
            if (connection.isClosed())
                openConnection();
        } 
        else {
            openConnection();
        }
    } catch (SQLException e) {
            System.out.println("DatabaseManager.getConnection() : " + e);   
        return null;
    }
    return connection;
}
public static String getDriverVersion() {

    String version = "exception";
    try {
        version = ""+DriverManager.getDriver(URL).getMajorVersion();
        version += ".";
        version += DriverManager.getDriver(URL).getMinorVersion();

    } catch (SQLException e)
    {}

    return version;
}
public static void init(String JDBCDriver, String parURL, String parUserName, String parPassWord, int maxConnections) throws ClassNotFoundException, SQLException {

    Class.forName(JDBCDriver);
    URL = parURL;
    userName = parUserName;
    passWord = parPassWord;
    openConnection();
}
public static void openConnection() throws SQLException{

    connection = DriverManager.getConnection(URL, userName, passWord);
}
public static ResultSet readSQL(String query, int maxresults, int maxtries) {

    if(maxtries == 0) {
        System.out.println("SQL query failed!");
        System.out.println("Query: "+query);
        return null;
    }

    int pos = 0;
    try{
        Connection connection = DatabaseManager.getConnection();
        pos = 1;
        Statement sm = connection.createStatement();
        pos = 2;
        sm.setMaxRows(maxresults);
        pos = 3;
        ResultSet rs = sm.executeQuery(query);
        pos = 4;
        return rs;
    }
    catch(Exception e) {

        System.out.println("Exception in DatabaseManager.readSQL ("+pos+"): " + e);
        System.out.println("SQL connection lost! Tries left:"+(maxtries-1));
        return readSQL(query, maxresults, maxtries-1);
    }
}
}

有谁知道为什么它告诉我 url 是空的?

如果您需要更多脚本,请告诉我!

4

0 回答 0