Export to GitHub

osjava - issue #15

Something wrong when simple-jndi load configuration


Posted on Apr 22, 2012 by Swift Hippo

I use java.util.Properties to store properties to a properties file. And use simple-jndi load it as a configuration in my application. But an exception was throwed.

Here is the exception output: Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc\:mysql\://localhost\:3306/statdb at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.osjava.sj.loader.SJDataSource.getConnection(SJDataSource.java:106) at org.osjava.sj.loader.SJDataSource.getConnection(SJDataSource.java:82) at test.Test.main(Test.java:135)

Here is the code: File tmpdir = new File("tmp"); if (!tmpdir.exists()) { tmpdir.mkdirs(); } File file = new File(tmpdir, "localhost.properties"); if (!file.exists()) { file.createNewFile(); } Properties fileprops = new Properties(); fileprops.put("StatDS/type", "javax.sql.DataSource"); fileprops.put("StatDS/driver", "com.mysql.jdbc.Driver"); fileprops.put("StatDS/url", "jdbc:mysql://localhost:3306/statdb"); fileprops.put("StatDS/user", "root"); fileprops.put("StatDS/password", "123456"); Writer writer = new OutputStreamWriter(new FileOutputStream(file)); fileprops.store(writer, "temp properties file"); writer.flush(); writer.close(); fileprops.load(new FileInputStream(file)); System.out.println(fileprops);

    Properties props = new Properties();
    String root = tmpdir.getAbsolutePath();
    props.put("org.osjava.sj.root", root);
    props.put("java.naming.factory.initial", "org.osjava.sj.SimpleContextFactory");
    props.put("org.osjava.sj.delimiter", "/");
    Context ctx = new SimpleContext(props);

    DataSource ds = (DataSource) ctx.lookup("localhost/StatDS");
    Connection conn = ds.getConnection();
    System.out.println(conn.toString());

Attachment is the output file.

I read the code and found where the problem is. Actually, java.util.Properties would escape some characters when store to file. But org.osjava.sj.loader.util.CustomProperties does not unescape them when load from file. The characters, which are escaped, are found from this link: http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#store(java.io.Writer, java.lang.String)

(My english is pool. I wish there are not wrong expressions. thx)

Attachments

Status: New

Labels:
Type-Defect Priority-Medium