Lubos Programming Blog

Lubos Blog about Programming (mainly Java) and Computers

Archive for July, 2009

Creating database schema using JDBC

Posted by lubosp on July 28, 2009

Unfortunately there is no database agnostic way to create database schema in Java using ORM tools like Hibernate, and even JDBC doesn’t offer database agnostic way to create new database schema.

Here is a JDBC code I use to create new database schema for MySql and PostgreSql:

Statement stmt = null;
Connection con = null;
try {
String url = configuration.getProperty(“hibernate.connection.url”);
con = getConnection(url,
configuration.getProperty(“hibernate.connection.username”),
configuration.getProperty(“hibernate.connection.password”));

stmt = con.createStatement();
if (!con.getMetaData().getTables(null,”mydb”,”mytable”,null).next()){
if (url.startsWith(“jdbc:postgresql”)) {
int count = stmt.executeUpdate(“CREATE DATABASE \”MyDb\”  WITH OWNER = ” +
configuration.getProperty(“hibernate.connection.username”));
} else if (url.startsWith(“jdbc:mysql”)) {
int count = stmt.executeUpdate(“CREATE SCHEMA mydb”);
} else {
throw new SQLException(“DB not supported”);
}
}
} catch (Exception e) {
// log failure
} finally {
if (stmt != null) try {stmt.close();} catch (Exception ex) {}
if (con != null) try {con.close();} catch (Exception ex) {}
}

private static Connection getConnection(String url, String user, String password) throws SQLException {
String urlPart = getUrlPart(url);
return DriverManager.getConnection(urlPart, user, password);
}

private static String getUrlPart(String url) {
int idx = url.lastIndexOf(‘/’);
if (idx < 0) return url; String part = url.substring(0, idx); if (url.startsWith("jdbc:postgresql")) { return part + "/postgres"; } else if (url.startsWith("jdbc:mysql")) { return part + "/mysql"; } return url; } [/sourcecode] Let me know if you find better way to create database schema in Java.

Posted in java | Leave a Comment »

Platform dependent line separator handling in Swing JTextArea

Posted by lubosp on July 25, 2009

If you wish to support platform dependent line separator handling in Swing JTextArea you have to use its read() and write() methods to load or persist the text in the JTextArea. Internally text area remembers the line separators and converts all of them to \n. When the text area is saved into file using write(), the line separators are converted to its original value.

Here is sample code to read the file into text area:

public class MyTextArea extends JTextArea {
...

try {
 FileInputStream fis = new FileInputStream(sourceFile);
 reader = new InputStreamReader(fis);
 read(reader, null);
} finally {
 if (reader != null) {
 try {
 reader.close();
 } catch (Exception ex) {
 // log exception
 }
 }
 }
}

Here is sample code to write text area into a file:

<pre>
<pre>public class MyTextArea extends JTextArea {
...</pre>
</pre>
try {
 writer = new OutputStreamWriter(new FileOutputStream(newFile));
 write(writer);
 } finally {
 if (writer != null) {
 try {
 writer.close();
 } catch (Exception ex) {
 // log exception
 }
 }
 }
}

Do not read the file content using Reader directly to get the content of the file and than call JTextArea.setText() it will not remember the line separator and it will keep the original line separator in the text area, so for example if the file uses Windows line separator \r\n, you will have to press Delete/Backspace key twice to delete line terminator. Pressing Enter key will insert \n and might cause inconsistent line separators (e.g. mixture of \r\n and \n) in the file when saved.

Do not write the file content using Writer directly to save the content of the text area in the file and after call JTextArea.getText(), it  would save the line separators as they are currently in the text area without converting them to the original state.

Check this thread for more info.

Posted in swing | Tagged: , | Leave a Comment »

Excellent Groovy presentation

Posted by lubosp on July 25, 2009

Excellent presentation about Groovy patterns highlighting advantages of Groovy:
Seven Groovy usage patterns for Java developers | Software Development Videos

Posted in java | Leave a Comment »