Commit b6a3931b authored by Matija Obreza's avatar Matija Obreza
Browse files

JDBC updates

parent 30c8e356
......@@ -218,9 +218,11 @@ public class DatabaseDialog extends Dialog {
String jdbcPath = workspacePath + "/jdbc";
try {
DownloadUtils.downloadFile(driver.getDownloadUrl(), jdbcPath);
SwtUtil.showMessageBox(getParent(), "Success!", "Driver downloaded from\n" + driver.getDownloadUrl());
extraClassLoader.addJars(new File(jdbcPath));
} catch (IOException e) {
_log.error(e.getMessage());
SwtUtil.showMessageBox(getParent(), "JDBC Driver", e.getMessage());
}
}
......
......@@ -38,8 +38,11 @@ public class ExtraClassLoader extends URLClassLoader {
}
private void addJar(File jarFile) throws MalformedURLException {
URL url = new URL("jar:file://" + jarFile.getAbsolutePath() + "!/");
_log.info("Adding JAR file " + url);
addURL(url);
URL fileUrl = jarFile.toURI().toURL();
System.err.println("URL: " + fileUrl);
URL url=new URL("jar:" + fileUrl.toString() + "!/");
//new URL("jar:file://" + jarFile.getAbsolutePath() + "!/");
_log.info("Adding JAR file " + fileUrl);
addURL(fileUrl);
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@ public class JdbcDrivers extends AbstractModelObject {
{
jdbcDrivers.add(new JdbcDriver("MySQL", "com.mysql.jdbc.Driver", mySqlDownloadUrl, "jdbc:mysql://localhost:3306/DATABASE"));
jdbcDrivers.add(new JdbcDriver("MsSQL", "com.microsoft.sqlserver.jdbc.SQLServerDriver", msSqlDownloadUrl, "jdbc:microsoft:sqlserver://localhost:1433/DATABASE"));
jdbcDrivers.add(new JdbcDriver("MsSQL", "com.microsoft.sqlserver.jdbc.SQLServerDriver", msSqlDownloadUrl, "jdbc:sqlserver://localhost\\INSTANCE:1433;databaseName=DATABASE"));
jdbcDrivers.add(new JdbcDriver("PostgreSQL", "org.postgresql.Driver", postgreSqlDownloadUrl, "jdbc:postgresql://localhost:5432/DATABASE"));
jdbcDrivers.add(new JdbcDriver("JDBC ODBC", "sun.jdbc.odbc.JdbcOdbcDriver", jdbcOdbcDownloadUrl, "jdbc:odbc://JDBC-ODBC-CONNECTSTRING"));
......
......@@ -23,15 +23,30 @@ public class ConnectionUtils {
_instance = this;
}
public static Connection getConnection(String driverClassName,String url, String user, String password) throws ClassNotFoundException, SQLException {
public static Connection getConnection(String driverClassName, String url, String user, String password) throws ClassNotFoundException, SQLException {
Connection con = null;
try {
con = DriverManager.getConnection(url, user, password);
con.setReadOnly(true);
return con;
} catch (Throwable e) {
System.err.println(e.getClass() + ": " + e.getMessage());
e.printStackTrace();
// Try the rest
}
try {
String driver = driverClassName;
Driver d = (Driver) Class.forName(driver, true, _instance.jdbcDriverClassLoader).newInstance();
System.out.println("Instance of " + d.getClass() + " created!");
DriverManager.registerDriver(new DriverShim(d));
System.out.println("Driver registered");
con = DriverManager.getConnection(url, user, password);
con.setReadOnly(true);
System.out.println("Got connection " + con);
} catch (ClassNotFoundException cnfe) {
throw cnfe;
} catch (SQLException sqe) {
......@@ -91,7 +106,7 @@ public class ConnectionUtils {
public boolean jdbcCompliant() {
return driver.jdbcCompliant();
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("getParentLogger() is Java 7");
}
......
package org.genesys2.anno.util;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.genesys2.anno.gui.SwtUtil;
import java.io.File;
import java.io.FileOutputStream;
......@@ -10,12 +12,11 @@ import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadUtils {
private static final Logger _log = Logger.getLogger(DownloadUtils.class);
private static final Logger _log = Logger.getLogger(DownloadUtils.class);
private static final int BUFFER_SIZE = 4096;
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
......@@ -46,20 +47,16 @@ public class DownloadUtils {
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
IOUtils.copy(inputStream, outputStream);
outputStream.close();
inputStream.close();
_log.info("File downloaded");
} else {
_log.error("No file to download. Server replied HTTP code: " + responseCode);
}
}
httpConn.disconnect();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment