Skip to content

Instantly share code, notes, and snippets.

@l-gu
Created June 18, 2014 21:22
Show Gist options
  • Save l-gu/ed0c8726807e5e8dd83a to your computer and use it in GitHub Desktop.
Save l-gu/ed0c8726807e5e8dd83a to your computer and use it in GitHub Desktop.
Database connection in a TelosysTools template
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcTool {
private String driverClassName = null ;
private String driverJarFile = null ;
//-----------------------------------------------------------------------------
// Specific Class Loader ( Inner class )
//-----------------------------------------------------------------------------
private static class MyClassLoader extends URLClassLoader
{
public MyClassLoader (URL[] urls, java.lang.ClassLoader parentLoader )
{
//--- Call the URLClassLoader constructor
super(urls, parentLoader);
}
}
public void setDriverClassName(String value) {
driverClassName = value ;
}
public void setDriverJarFile(String value) {
driverJarFile = value ;
}
/**
* @param driverClassName
* @param driverJarFile
* @return
*/
public Driver getDriver(String driverClassName, String driverJarFile) {
if ( driverClassName == null ) {
throw new RuntimeException("Cannot get driver: driverClassName is null");
}
if ( driverJarFile == null ) {
throw new RuntimeException("Cannot get driver: driverJarFile is null");
}
URL[] urls = new URL[1];
try
{
// urls[n] = new File(sPath).toURL(); // toURL deprecated since Java 5.0
// The recommended new code ( see JavaDoc )
URI uri = new File(driverJarFile).toURI();
urls[0] = uri.toURL();
}
catch (MalformedURLException e)
{
throw new RuntimeException("Cannot convert '" + driverJarFile + "' to URL (MalformedURLException)", e);
}
ClassLoader parentLoader = ClassLoader.getSystemClassLoader();
MyClassLoader loader = new MyClassLoader ( urls, parentLoader );
Class<?> c = null ;
try {
c = loader.loadClass(driverClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class not found '" + driverClassName + "' ", e);
}
Driver driver = null ;
try {
driver = (Driver) c.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Cannot instanciate driver '" + driverClassName + "' ", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Illegal access / driver '" + driverClassName + "' ", e);
}
return driver ;
}
public Connection getConnection() {
Driver driver = getDriver(this.driverClassName, this.driverJarFile);
Connection connection = null ;
try {
Properties prop = new Properties() ;
prop.put("user", "root" ) ;
prop.put("password", "admin" ) ;
connection = driver.connect("jdbc:derby://localhost:1527/bookstore", prop);
} catch (SQLException e) {
throw new RuntimeException("Cannot get connection", e);
}
return connection ;
}
public Connection getConnection(String url, String user, String password ) {
Driver driver = getDriver(this.driverClassName, this.driverJarFile);
Connection connection = null ;
try {
Properties prop = new Properties() ;
prop.put("user", user ) ;
prop.put("password", password ) ;
connection = driver.connect(url, prop);
} catch (SQLException e) {
throw new RuntimeException("Cannot get connection", e);
}
return connection ;
}
}
@fbockelee
Copy link

Hi,
Thx for the click reply. Thats clearly better ;-)
For TAGS, it may be a solution.
best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment