Created
June 18, 2014 21:22
-
-
Save l-gu/ed0c8726807e5e8dd83a to your computer and use it in GitHub Desktop.
Database connection in a TelosysTools template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; | |
} | |
} |
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
Hi,
Your specific classes must be located in the 'classes' folder (in the templates bundle)
or in a jar located in the 'lib' folder (in the templates bundle)
For example :
TelosysTools/templates/mybundle/classes : mypackage/xx.class
TelosysTools/templates/mybundle/lib : xx.jar
See the "$loader" object documentation : https://www.telosys.org/templates-doc/objects/loader.html
See also the examples provided in the budle "advanced-templates-samples-T300" :
https://github.com/telosys-templates-v3/advanced-templates-samples-T300
FYI : if you are using a DSL model, the upcoming version will provide "TAGS" to define specific information
A tag is an string starting with "#" with or without information in parenthesis
something like "#attribute1(info)" or "#mytag"
All these tags will be usable in templates
It could be a solution for your specific needs