Advertisement
Zizalik

CurrencyDATABASE

May 16th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package DB;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.util.Properties;
  16.  
  17. /**
  18. *
  19. * @author t166156
  20. */
  21. public class Database {
  22.  
  23. /**
  24. * @param args the command line arguments
  25. */
  26. private static final String CREATE_TABLE_QUERY
  27. = " create table Currency("
  28. + " id int primary key,"
  29. + " ShortName varchar(10) not null,"
  30. + " FullName varchar(50) not null,"
  31. + " Unit varchar(50) not null,"
  32. + " Symbol varchar(10) not null,"
  33. + " \"Country\" int)";
  34. private static final String INSERT_DATA_QUERY
  35. = "insert into Currency values(?,?,?,?,?,?)";
  36. private static final String SELECT_DATA_QUERY
  37. = "select * from Currency";
  38.  
  39. public static void main(String[] args) {
  40. Connection conn = null;
  41. try {
  42. conn = getConnection();
  43. //create table
  44. conn.createStatement().executeUpdate(CREATE_TABLE_QUERY);
  45. PreparedStatement st = conn.prepareStatement(INSERT_DATA_QUERY);
  46. st.setInt(1, 1);
  47. st.setString(2, "ad");
  48. st.setString(3, "kkkkkkk");
  49. st.setString(4, "ja");
  50. st.setString(5, "aa");
  51. st.setInt(6, 1);
  52. st.executeUpdate();
  53. st.close();
  54.  
  55. } catch (SQLException ex) {
  56. System.err.println("Error1: " + ex.getMessage());
  57. }
  58. try {
  59. ResultSet rs = conn.createStatement().executeQuery(SELECT_DATA_QUERY);
  60. while (rs.next()) {
  61. String id = rs.getString("id");
  62. String shortname = rs.getString("ShortName");
  63. String fullname = rs.getString("FullName");
  64. String unit = rs.getString("Unit");
  65. String symbol = rs.getString("Symbol");
  66.  
  67. String output = "\nCurrency:\n Id:%s\n shortname:%s\n fullname:%s\n Unit:%s\n Symbol:%s";
  68. System.out.println(String.format(output, id, shortname, fullname, unit, symbol));
  69.  
  70. }
  71. rs.close();
  72. conn.close();
  73. } catch (SQLException ex) {
  74. System.err.println("Error2: " + ex.getMessage());
  75. }
  76. }
  77.  
  78. public static Connection getConnection() {
  79. Properties props = new Properties();
  80. FileInputStream fis = null;
  81. Connection conn = null;
  82. try {
  83. fis = new FileInputStream("derbydb.properties");
  84. props.load(fis);
  85.  
  86. Class.forName(props.getProperty("DATABASE_DRIVER"));
  87.  
  88. conn = DriverManager.getConnection(props.getProperty("DATABASE_URL"),
  89. props.getProperty("DATABASE_USERNAME"),
  90. props.getProperty("DATABASE_PASSWORD"));
  91. } catch (IOException | ClassNotFoundException | SQLException ex) {
  92. System.err.println("Error: " + ex.getMessage());
  93. }
  94. return conn;
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement