こんにちは、私はこのコードを持っています:
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 を null にすることはできません
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が空であると表示される理由を知っている人はいますか?
さらにスクリプトが必要な場合はお知らせください。