Created
March 28, 2009 15:54
-
-
Save myabc/87129 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
diff --git a/do_jdbc/src/java/data_objects/Connection.java b/do_jdbc/src/java/data_objects/Connection.java | |
index bce16f2..bc4c310 100644 | |
--- a/do_jdbc/src/java/data_objects/Connection.java | |
+++ b/do_jdbc/src/java/data_objects/Connection.java | |
@@ -8,6 +8,7 @@ import java.sql.SQLException; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
+import java.util.Properties; | |
import java.util.StringTokenizer; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
@@ -77,6 +78,7 @@ public class Connection extends RubyObject { | |
// System.out.println("============== initialize called " + uri); | |
Ruby runtime = recv.getRuntime(); | |
String jdbcDriver = null; | |
+ String encoding = null; | |
java.net.URI connectionUri; | |
try { | |
@@ -107,6 +109,10 @@ public class Connection extends RubyObject { | |
jdbcDriver = query.get("driver"); | |
//String protocol = query.get("protocol"); // XXX : not sure of the point of this | |
+ encoding = query.get("encoding"); | |
+ if (encoding == null) { | |
+ encoding = query.get("charset"); | |
+ } | |
} | |
// Load JDBC Driver Class | |
@@ -125,6 +131,12 @@ public class Connection extends RubyObject { | |
// DriverManager.registerDriver(driver); | |
} | |
+ // default encoding to UTF-8, if not specified | |
+ // TODO: encoding should be mapped from Ruby encoding type to Java encoding | |
+ if (encoding == null) { | |
+ encoding = "utf8"; | |
+ } | |
+ | |
java.sql.Connection conn; | |
try { | |
@@ -149,18 +161,32 @@ public class Connection extends RubyObject { | |
userInfo += ":"; | |
} | |
if (!jdbcUri.startsWith("jdbc:")) { | |
- jdbcUri = "jdbc:" + jdbcUri; | |
+ jdbcUri = "jdbc:" + jdbcUri + "?&characterEncoding=utf8&useUnicode=yes"; | |
} | |
+ String username = userInfo.substring(0, userInfo.indexOf(":")); | |
+ String password = userInfo.substring(userInfo.indexOf(":") + 1); | |
+ | |
+ Properties props = new Properties(); | |
+ props.put("user", username); | |
+ props.put("password", password); | |
+ props.put("useUnicode", "yes"); | |
- conn = DriverManager.getConnection(jdbcUri, | |
- userInfo.substring(0, userInfo.indexOf(":")), | |
- userInfo.substring(userInfo.indexOf(":") + 1)); | |
+ if ("latin1".equals(encoding)) { | |
+ encoding = "ISO8859_1"; | |
+ } | |
+ props.put("characterEncoding", encoding); // change this for other DB | |
+ | |
+ conn = DriverManager.getConnection(jdbcUri, props); | |
} else { | |
String jdbcUri = connectionUri.toString(); | |
if (!jdbcUri.startsWith("jdbc:")) { | |
jdbcUri = "jdbc:" + jdbcUri; | |
} | |
- conn = DriverManager.getConnection(jdbcUri); | |
+ | |
+ Properties props = new Properties(); | |
+ props.put("characterEncoding", encoding); // change this for other DB | |
+ | |
+ conn = DriverManager.getConnection(jdbcUri, props); | |
} | |
} catch (SQLException ex) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment