Guest User

Untitled

a guest
Jun 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package budgettst;
  7.  
  8. /**
  9. *
  10. * @author frih0812
  11. */
  12.  
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.sql.Statement;
  18. import java.sql.ResultSetMetaData;
  19. import java.util.Date;
  20.  
  21. public class DbConnection
  22. {
  23. private static String dbURL = "jdbc:derby://localhost:1527/EkonomiKoll;create=true;user=tst;password=tst";
  24. private static String tableName = "Utgifter";
  25. // jdbc Connection
  26. private static Connection conn = null;
  27. private static Statement stmt = null;
  28.  
  29. public static void main(String[] args)
  30. {
  31. createConnection();
  32. //insertRestaurants(5, "LaVals", "Berkeley");
  33. //insertRestaurants(20, "Mias grill", "Umeå");
  34. //insertUtgifter(7, "Invito", "Umeå");
  35. //selectUtgifter();
  36. shutdown();
  37. }
  38.  
  39.  
  40.  
  41. int id,summa;
  42. String kategori,notering;
  43. Date datum;
  44.  
  45. public String Utgifter(Integer id, int summa, Date datum, String kategori,String notering)
  46. {
  47. this.id = id;
  48. this.summa = summa;
  49. this.datum = datum;
  50. this.kategori = kategori;
  51. this.notering = notering;
  52. //insertUtgifter(id,summa,datum,kategori,notering);
  53. String sqlSats="insert into Utgifter (id, summa, datum, kategori, notering) values ("+
  54. id +", "+ summa +", "+"'"+ datum +"', "+"'"+ kategori +"', "+"'"+ notering+"'"+")";
  55. return(sqlSats);
  56. //insertUtgifter(sqlSats);
  57. }
  58.  
  59. /*public Utgifter()
  60. {
  61. }
  62.  
  63. public Utgifter(Integer id)
  64. {
  65. this.id = id;
  66. }
  67.  
  68. public Utgifter(Integer id, int summa, Date datum, String kategori)
  69. {
  70. this.id = id;
  71. this.summa = summa;
  72. this.datum = datum;
  73. this.kategori = kategori;
  74. }
  75.  
  76. public Integer getId()
  77. {
  78. return id;
  79. }
  80.  
  81. public void setId(Integer id)
  82. {
  83. this.id = id;
  84. }
  85.  
  86. public int getSumma()
  87. {
  88. return summa;
  89. }
  90.  
  91. public void setSumma(int summa)
  92. {
  93. this.summa = summa;
  94. }
  95.  
  96. public Date getDatum()
  97. {
  98. return datum;
  99. }
  100.  
  101. public void setDatum(Date datum)
  102. {
  103. this.datum = datum;
  104. }
  105.  
  106. public String getNotering()
  107. {
  108. return notering;
  109. }
  110.  
  111. public void setNotering(String notering)
  112. {
  113. this.notering = notering;
  114. }
  115.  
  116. public String getKategori()
  117. {
  118. return kategori;
  119. }
  120.  
  121. public void setKategori(String kategori)
  122. {
  123. this.kategori = kategori;
  124. }*/
  125.  
  126.  
  127.  
  128. public static void createConnection()
  129. {
  130. try
  131. {
  132. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  133. //Get a connection
  134. conn = DriverManager.getConnection(dbURL);
  135. System.out.println("Hallelulja!");
  136. }
  137. catch (Exception except)
  138. {
  139. except.printStackTrace();
  140. System.out.println("ASS!!");
  141. }
  142.  
  143. }
  144.  
  145. public static void insertUtgifter(String sqlSats)
  146. {
  147. try
  148. {
  149. stmt = conn.createStatement();
  150. stmt.execute(sqlSats);
  151. stmt.close();
  152. }
  153. catch (SQLException sqlExcept)
  154. {
  155. sqlExcept.printStackTrace();
  156. }
  157. shutdown();
  158. }
  159.  
  160. private static void selectUtgifter()
  161. {
  162. try
  163. {
  164. stmt = conn.createStatement();
  165. ResultSet results = stmt.executeQuery("select * from " + tableName);
  166. ResultSetMetaData rsmd = results.getMetaData();
  167. int numberCols = rsmd.getColumnCount();
  168. for (int i=1; i<=numberCols; i++)
  169. {
  170. //print Column Names
  171. System.out.print(rsmd.getColumnLabel(i)+"\t\t");
  172. }
  173.  
  174. System.out.println("\n-------------------------------------------------");
  175.  
  176. while(results.next())
  177. {
  178. int id = results.getInt(1);
  179. String restName = results.getString(2);
  180. String cityName = results.getString(3);
  181. System.out.println(id + "\t\t" + restName + "\t\t" + cityName);
  182. }
  183. results.close();
  184. stmt.close();
  185. }
  186. catch (SQLException sqlExcept)
  187. {
  188. sqlExcept.printStackTrace();
  189. }
  190. }
  191.  
  192. private static void shutdown()
  193. {
  194. try
  195. {
  196. if (stmt != null)
  197. {
  198. stmt.close();
  199. }
  200. if (conn != null)
  201. {
  202. DriverManager.getConnection(dbURL + ";shutdown=true");
  203. conn.close();
  204. }
  205. }
  206. catch (SQLException sqlExcept)
  207. {
  208.  
  209. }
  210. }
  211. }
Add Comment
Please, Sign In to add comment