thufir

map result set to bean

Jul 5th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. thufir@dur:~$
  2. thufir@dur:~$ java -jar NetBeansProjects/Server/dist/Server.jar
  3. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  4. INFO: id    4
  5. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  6. INFO: title 12
  7. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  8. INFO: phone 4
  9. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  10. INFO: state_timestamp   93
  11. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  12. INFO: created   93
  13. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  14. INFO: state 5
  15. Jul 05, 2014 7:43:21 PM net.bounceme.dur.db.Query metaData
  16. INFO: user_id   5
  17. Jul 05, 2014 7:43:21 PM net.bounceme.dur.server.Driver startServer
  18. INFO: started server
  19. ^Cthufir@dur:~$
  20. thufir@dur:~$
  21. thufir@dur:~$ mysql -u root -p
  22. Enter password:
  23. Welcome to the MySQL monitor.  Commands end with ; or \g.
  24. Your MySQL connection id is 83
  25. Server version: 5.5.37-0ubuntu0.14.04.1 (Ubuntu)
  26.  
  27. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  28.  
  29. Oracle is a registered trademark of Oracle Corporation and/or its
  30. affiliates. Other names may be trademarks of their respective
  31. owners.
  32.  
  33. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  34.  
  35. mysql>
  36. mysql> describe titles.titles;
  37. +-----------------+-------------+------+-----+---------------------+----------------+
  38. | Field           | Type        | Null | Key | Default             | Extra          |
  39. +-----------------+-------------+------+-----+---------------------+----------------+
  40. | id              | int(11)     | NO   | PRI | NULL                | auto_increment |
  41. | created         | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
  42. | phone           | int(11)     | NO   |     | NULL                |                |
  43. | title           | varchar(45) | NO   |     | NULL                |                |
  44. | state           | smallint(6) | NO   |     | NULL                |                |
  45. | state_timestamp | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
  46. | user_id         | smallint(6) | NO   |     | NULL                |                |
  47. +-----------------+-------------+------+-----+---------------------+----------------+
  48. 7 rows in set (0.00 sec)
  49.  
  50. mysql>
  51. mysql> quit
  52. Bye
  53. thufir@dur:~$        
  54. thufir@dur:~$ cat NetBeansProjects/Server/src/net/bounceme/dur/db/Query.java
  55. package net.bounceme.dur.db;
  56.  
  57. import java.sql.Connection;
  58. import java.sql.DatabaseMetaData;
  59. import java.sql.DriverManager;
  60. import java.sql.PreparedStatement;
  61. import java.sql.ResultSet;
  62. import java.sql.SQLException;
  63. import java.util.HashMap;
  64. import java.util.List;
  65. import java.util.Map;
  66. import java.util.logging.Level;
  67. import java.util.logging.Logger;
  68. import net.bounceme.dur.server.MyProps;
  69. import net.bounceme.dur.server.RecordsQueueWrapper;
  70. import net.bounceme.dur.server.Title;
  71.  
  72. public class Query {
  73.  
  74.     private static final Logger log = Logger.getLogger(Query.class.getName());
  75.     private RecordsQueueWrapper recordsQueue = RecordsQueueWrapper.getInstance();
  76.     String driver, url, user, password;
  77.  
  78.     public Query(MyProps props) {
  79.         driver = props.getDbDriver();
  80.         url = props.getDbUrl();
  81.         user = props.getDbUser();
  82.         password = props.getDbPassword();
  83.     }
  84.  
  85.     public void rows() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
  86.         Connection connection = connect();
  87.         PreparedStatement statement = connection.prepareStatement("SELECT * FROM Titles.titles");
  88.         ResultSet resultSet = statement.executeQuery();
  89.     }
  90.  
  91.     public void metaData() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
  92.         Connection c = null;
  93.         c = connect();
  94.         String catalog = null;
  95.         String schemaPattern = null;
  96.         String tableNamePattern = "titles";
  97.         String columnNamePattern = null;
  98.         DatabaseMetaData meta = c.getMetaData();
  99.         ResultSet result = meta.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
  100.         Map<String, Integer> map = new HashMap<>();
  101.         while (result.next()) {
  102.             map.put(result.getString(4), result.getInt(5));
  103.         }
  104.         for (Map.Entry<String, Integer> entry : map.entrySet()) {
  105.             String key = entry.getKey();
  106.             Integer value = entry.getValue();
  107.             log.log(Level.INFO, "{0}\t{1}", new Object[]{key, value});
  108.         }
  109.  
  110.     }
  111.  
  112.     private Connection connect() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
  113.         log.fine("connecting...");
  114.         Connection connection = null;
  115.         Class.forName(driver).newInstance();
  116.         connection = DriverManager.getConnection(url, user, password);
  117.         log.fine("...connected");
  118.         return connection;
  119.     }
  120. }
  121. thufir@dur:~$
  122. thufir@dur:~$
Advertisement
Add Comment
Please, Sign In to add comment