KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > samples > SecurePassword


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.samples;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DriverManager JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.Statement JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 /**
14  * @author Thomas
15  */

16
17 public class SecurePassword {
18     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
19         
20         Class.forName("org.h2.Driver");
21         String JavaDoc url = "jdbc:h2:data/simple";
22         String JavaDoc user = "sam";
23         char[] password = {'t', 'i', 'a', 'E', 'T', 'r', 'p'};
24         
25         // 'unsafe' way to connect
26
// the password may reside in the main memory for an undefined time
27
// or even written to disk (swap file)
28
// Connection conn = DriverManager.getConnection(url, user, new String(password));
29

30         // 'safe' way to connect
31
// the password is overwritten after use
32
Properties JavaDoc prop = new Properties JavaDoc();
33         prop.setProperty("user", user);
34         prop.put("password", password);
35         Connection JavaDoc conn = DriverManager.getConnection(url, prop);
36         
37         Statement JavaDoc stat = conn.createStatement();
38         stat.execute("DROP TABLE IF EXISTS TEST");
39         stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
40         stat.executeUpdate("INSERT INTO TEST VALUES(1, 'Hello')");
41         ResultSet JavaDoc rs = stat.executeQuery("SELECT * FROM TEST");
42         while(rs.next()) {
43             System.out.println(rs.getString("NAME"));
44         }
45         conn.close();
46     }
47         
48 }
49
Popular Tags