Advertisement
ExaGridDba

urandom fix for JDBC

Jul 20th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.sql.SQLException;
  2. import java.sql.DriverManager;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.util.Formatter;
  7. import java.util.Locale;
  8.  
  9. public class JdbcTest {
  10.  
  11.         public static void main(String[] args) throws SQLException {
  12.  
  13.                 System.setProperty("java.security.egd", "file:///dev/urandom");
  14.                 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  15.  
  16.                 String url = "//192.168.1.17:1521/wailua";
  17.                 String username = "sh";
  18.                 String password = "sh123";
  19.  
  20.                 System.out.println("connecting.");
  21.                 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + url, username, password);
  22.  
  23.                 System.out.println("Total sales by country");
  24.  
  25.                 String sql = "  select c.country_name, sum (amount_sold) as dollar_sales"
  26.                         + "    from sales s"
  27.                         + "         join customers cus"
  28.                         + "            on s.cust_id = cus.cust_id"
  29.                         + "         join countries c"
  30.                         + "            on cus.country_id = c.country_id"
  31.                         + " group by c.country_name"
  32.                         + " order by dollar_sales desc";
  33.  
  34.                 PreparedStatement pstmt = conn.prepareStatement( sql );
  35.  
  36.                 pstmt.execute();
  37.  
  38.                 StringBuilder sb = new StringBuilder();
  39.                 Formatter formatter = new Formatter(sb, Locale.US);
  40.                 ResultSet rs = pstmt.getResultSet();
  41.                 while ( rs.next() )
  42.                 {
  43.                         sb.setLength( 0 );
  44.                         System.out.println( formatter.format("%-30s %15.2f",
  45.                                 rs.getString( 1 ), rs.getFloat( 2 )) );
  46.                 }
  47.         }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement