Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. import javax.management.Query;
  2. import java.sql.*;
  3. import java.util.LinkedList;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8. static Connection conn = SQLOperations.getConnection();
  9.  
  10. static String[] menu = {
  11. "------------ MENIU -------------",
  12. "0. Iseiti",
  13. "1. Prideti darbuotoja",
  14. "2. Perziureti darbuotojus",
  15. "3. Pakeisti darbuotojo informacija",
  16. "4. Panaikinti darbuotoja",
  17. "5. Perziureti informacija apie keliones"
  18. };
  19.  
  20. public static void main(String[] args) {
  21.  
  22. int next;
  23. Scanner input = new Scanner(System.in);
  24.  
  25. SQLOperations.loadDriver();
  26.  
  27. for(String s : menu){
  28. System.out.println(s);
  29. }
  30. System.out.println("Iveskite pasirinkima:");
  31. next = input.nextInt();
  32. input.nextLine();
  33. while(true){
  34. switch(next){
  35. case 0:
  36. SQLOperations.closeConnection(conn);
  37. System.exit(1);
  38. break;
  39. case 1:
  40. System.out.println("Iveskite pardavejo AK, Varda, Pavarde ir Tel. Nr.");
  41. System.out.println("AK:");
  42. String AK = input.nextLine();
  43. System.out.println("Vardas:");
  44. String vardas = input.nextLine();
  45. System.out.println("Pavarde:");
  46. String pavarde = input.nextLine();
  47. System.out.println("Telefono Nr.:");
  48. String telnr = input.nextLine();
  49. try {
  50. SQLOperations.insertToDB(conn, "INSERT INTO Darbuotojas VALUES('" + AK + "','" + vardas + "','" + pavarde + "','" + telnr
  51. + "')");
  52. System.out.println("Prideta!");
  53. }catch(SQLException e){
  54. System.out.println("Prideti nepavyko!");
  55. System.out.println(e.toString());
  56. }
  57. break;
  58. case 2:
  59. displayList("SELECT * FROM Darbuotojas");
  60. break;
  61. case 3:
  62. displayList("SELECT * FROM Darbuotojas");
  63. System.out.println("Iveskite AK darbuotojo, kuri norite modifikuoti");
  64. String ak = input.nextLine();
  65. System.out.println("Pasirinkite ka norite keisti: 1 - AK, 2 - Varda, 3 - Pavarde, 4 - Tel. Nr.");
  66. int read = input.nextInt();
  67. System.out.println("Iveskite nauja duomeni");
  68. String newData = input.nextLine();
  69. String target = "";
  70. switch(read){
  71. case 1:
  72. target = "AK";
  73. break;
  74. case 2:
  75. target = "Vardas";
  76. break;
  77. case 3:
  78. target = "Pavarde";
  79. break;
  80. case 4:
  81. target = "TelNr";
  82. break;
  83. default:
  84. System.out.println("Blogas skaicius, operacija nutraukiama");
  85. continue;
  86. }
  87. try{
  88. SQLOperations.insertToDB(conn,"UPDATE Darbuotojas SET " + target + " = '"+newData+"' WHERE AK = '" + ak + "'");
  89. }catch(SQLException e){
  90. System.out.println(e.toString());
  91. }
  92. break;
  93. case 4:
  94. displayList("SELECT * FROM Darbuotojas");
  95. System.out.println("Pasirinkite AK darbuotojo, kuri norite pasalinti");
  96. System.out.println("AK: ");
  97. String darbAK = input.nextLine();
  98. try{
  99. SQLOperations.insertToDB(conn, "DELETE FROM Darbuotojas WHERE AK = '" + darbAK + "'");
  100. }catch(SQLException e){
  101. System.out.println(e.toString());
  102. }
  103. break;
  104. case 5:
  105. displayList("SELECT * FROM Pirkejas");
  106. break;
  107. default:
  108. System.out.println("Ivedete bloga skaiciu!");
  109. }
  110. System.out.println("Iveskite kita pasirinkima");
  111. next = input.nextInt();
  112. input.nextLine();
  113. }
  114.  
  115. }
  116.  
  117. private static void displayList(String query){
  118. LinkedList<LinkedList<String>> res = SQLOperations.getFromDB(conn, query);
  119. for(LinkedList<String> list : res){
  120. if(list.size() == 4) {
  121. System.out.println("AK: " + list.get(0).trim());
  122. System.out.println("Vardas: " + list.get(1).trim());
  123. System.out.println("Pavarde: " + list.get(2).trim());
  124. System.out.println("Tel nr: " + list.get(3).trim());
  125.  
  126. }else{
  127. System.out.println("ID: " + list.get(0).trim());
  128. System.out.println("Darbuotojo AK: " + list.get(1).trim());
  129. System.out.println("AK: " + list.get(2).trim());
  130. System.out.println("Vardas: " + list.get(3).trim());
  131. System.out.println("Pavarde: " + list.get(4).trim());
  132. System.out.println("Tel nr: " + list.get(5).trim());
  133. System.out.println("Adresas: " + list.get(6).trim());
  134.  
  135. }
  136. }
  137. }
  138. }
  139.  
  140. class SQLOperations{
  141. public static void insertToDB(Connection conn, String query) throws SQLException{
  142. Statement stmt = null;
  143.  
  144. try{
  145. stmt = conn.createStatement();
  146. stmt.execute(query);
  147. }catch(SQLException e){
  148. throw e;
  149. }finally {
  150. try {
  151. if(stmt != null) stmt.close();
  152. } catch (SQLException e) {
  153. System.out.println("Netiketa SQL klaida!");
  154. }
  155. }
  156. }
  157.  
  158. public static LinkedList<LinkedList<String>> getFromDB(Connection conn, String query){
  159. LinkedList<LinkedList<String>> result = new LinkedList<>();
  160. Statement stmt = null;
  161. ResultSet rslt = null;
  162.  
  163. try{
  164. stmt = conn.createStatement();
  165. rslt = stmt.executeQuery(query);
  166.  
  167. while(rslt.next()){
  168. LinkedList<String> row = new LinkedList<>();
  169. for(int i = 1; i<= rslt.getMetaData().getColumnCount();i++){
  170. String tmp = rslt.getString(i);
  171. if(tmp.equals(null)) tmp = "";
  172. row.add(tmp);
  173. }
  174. result.add(row);
  175. }
  176. }catch(SQLException e){
  177. System.out.println(e.toString());
  178. }finally{
  179. try{
  180. if(rslt != null) rslt.close();
  181. if(stmt != null) stmt.close();
  182. }catch(SQLException e){
  183. System.out.println(e.toString());
  184. }
  185. }
  186.  
  187. return result;
  188. }
  189.  
  190.  
  191.  
  192.  
  193. public static void loadDriver(){
  194. try {
  195. Class.forName("org.postgresql.Driver");
  196. }
  197. catch (ClassNotFoundException cnfe) {
  198. System.out.println("Couldn't find driver class!");
  199. cnfe.printStackTrace();
  200. System.exit(1);
  201. }
  202. }
  203.  
  204. public static Connection getConnection() {
  205. Connection postGresConn = null;
  206. try {
  207. postGresConn = DriverManager.getConnection("jdbc:postgresql://pgsql2.mif/studentu", "maju3327", "maju3327") ;
  208. }
  209. catch (SQLException sqle) {
  210. System.out.println("Nepavyko prisijungti prie DB!");
  211. sqle.printStackTrace();
  212. return null ;
  213. }
  214.  
  215. return postGresConn ;
  216. }
  217.  
  218. public static void closeConnection(Connection conn){
  219. try{
  220. conn.close();
  221. }catch (SQLException exp){
  222. System.out.println("Nepavyko isjungti rysio!");
  223. exp.printStackTrace();
  224. }
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement