Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1.  
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.text.DateFormat;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15.  
  16.  
  17. public class DBCom {
  18. private String dbpath = "jdbc:postgresql://localhost:9505/postgres";
  19. private String user = "postgres";
  20. private String pass = "samurai87";
  21. private Connection con = null;
  22.  
  23. public DBCom(){
  24. }
  25.  
  26. /**
  27. * This method establishes a connection to the database specified at dbpath.
  28. *
  29. */
  30. public synchronized void openConnection(){
  31. try {
  32.  
  33. } catch (Exception e) {
  34. System.out.println(e);
  35. }
  36. }
  37.  
  38. /**
  39. * This method executes a SELECT statement and returns the ResultSet.
  40. * @throws SQLException
  41. */
  42. public synchronized ResultSet select(String statement) throws SQLException{
  43. ResultSet res = null;
  44. try{
  45. // openConnection(); //open
  46. Statement set = con.createStatement();
  47. res = set.executeQuery(statement);
  48. return res;
  49. }catch (Exception e) {
  50. System.out.println(e);
  51. }finally{
  52. // con.close(); //close
  53. }
  54. return null;
  55. }
  56.  
  57. /**
  58. * This method takes a statement like INSERT, UPDATE or DELETE and executes them.
  59. * @param statement
  60. * @throws SQLException
  61. */
  62. public synchronized boolean update(String statement) throws SQLException{
  63. try{
  64. // openConnection(); //open
  65. Statement set = con.createStatement();
  66. set.executeUpdate(statement);
  67. // set.close();
  68. return true;
  69. }catch (Exception e) {
  70. return false;
  71. }finally{
  72. // con.close(); //close
  73. }
  74. }
  75. /**
  76. * Regnskapssystemet er ikke integrert med det programmet du arbeider med.
  77. * Dataene skal overføres dit via tekstfiler. Tekstfilen har fire kolonner: prosjekt_id, kunde, tekst og beløp, atskilt med semikolon.
  78. * For prosjektarbeid skal alle timekostnadene for denne måneden og det aktuelle prosjektet summeres til én linje med tekst “timer”,
  79. * for prosjektkostnader skal listen inneholde en linje pr linje i tabellen for denne måneden. Listen skal være sortert på kunde og prosj_id.
  80. * Programmet skal sette faktura_sendt lik dagens dato i de to tabellene.
  81. * @throws ClassNotFoundException
  82. * @throws SQLException
  83. */
  84. public void fakturer(int mnd, String name) throws ClassNotFoundException, SQLException{
  85. try {
  86. Class.forName("org.postgresql.Driver");
  87. con = DriverManager.getConnection(dbpath, user, pass);
  88. con.setAutoCommit(false);
  89. con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  90.  
  91. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  92. String datef = formatter.format(new Date());
  93.  
  94. ResultSet res = select("select prosj_id, kunde, tekst, beløp, sum(timefaktor * ant_timer * timelønn) as beløp2" +
  95. " from prosjektkostnader k natural full join prosjektarbeid a left join prosjekt p using (prosj_id)" +
  96. " left join ansatt ans using (ans_id) where k.faktura_sendt is null and a.faktura_sendt is null" +
  97. " and (date_part('month', k.dato) = "+mnd+" or date_part('month', a.dato) = "+mnd+")" +
  98. " group by prosj_id, tekst, kunde, beløp order by kunde, prosj_id desc;");
  99. while(res.next()){
  100. int col1 = res.getInt(1);
  101. String col2 = res.getString(2);
  102. String col3 = res.getString(3);
  103.  
  104. if(col3 == null){ //prosjektarbeid
  105. double col5 = res.getDouble(5);
  106. String line = col1+";"+col2+";timer;"+col5;
  107. lagrePåFil(line, name);
  108. //oppdater dato
  109. update("UPDATE prosjektarbeid SET faktura_sendt = '"+datef+"' where prosj_id = "+col1+";");
  110. }else{ // prosjektkostnad
  111. double col4 = res.getDouble(4);
  112. String line = col1+";"+col2+";"+col3+";"+col4;
  113. lagrePåFil(line, name);
  114. //oppdater dato
  115. update("UPDATE prosjektkostnader SET faktura_sendt = '"+datef+"' where prosj_id = "+col1+";");
  116. }
  117. }
  118. con.commit();
  119. con.close();
  120. res.close();
  121. } catch (SQLException e) {
  122. try {
  123. con.rollback();
  124. } catch (Exception e2) {
  125. // TODO: handle exception
  126. }
  127. e.printStackTrace();
  128. } finally{
  129. con.close();
  130. }
  131. }
  132.  
  133. private synchronized void lagrePåFil(String line, String name) {
  134. try {
  135. PrintWriter skriver = new PrintWriter(new BufferedWriter(new FileWriter(name, true)));
  136. skriver.println(line);
  137. System.out.println("Lagret: " + line);
  138. skriver.close();
  139. } catch (IOException e) {
  140. System.out.println("Feil ved skriving fil: " + e);
  141. System.out.println("Teksten som skulle vært skrevet ut: " + line);
  142. e.printStackTrace();
  143. }
  144. }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement