Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1.  
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.*;
  7. import java.sql.ResultSet;
  8.  
  9.  
  10. public class DB {
  11.  
  12. public Connection con = null;
  13.  
  14. private PreparedStatement stmt_insertKomanda;
  15. private PreparedStatement stmt_updateKomanda;
  16. private PreparedStatement stmt_selectKomanda;
  17. private PreparedStatement stmt_deleteKomanda;
  18.  
  19. private PreparedStatement stmt_insertZaidejas;
  20. private PreparedStatement stmt_updateZaidejas;
  21. private PreparedStatement stmt_selectZaidejas;
  22. private PreparedStatement stmt_deleteZaidejas;
  23.  
  24. //--------------MANO----------------------
  25. private PreparedStatement stmt_insertLigonine;
  26. private PreparedStatement stmt_selectLigonine;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. public void loadDriver() throws Exception {
  33. try {
  34. Class.forName("org.postgresql.Driver");
  35. System.out.println("Driveris buvo sekmingai uzkrautas!");
  36. }
  37. catch (ClassNotFoundException cnfe) {
  38. throw new Exception("Unable to load the driver class!");
  39. }
  40. }
  41.  
  42. public Connection getConnection() throws Exception {
  43. try {
  44. con = DriverManager.getConnection("jdbc:postgresql://pgsql2.mif/studentu", "zyna2386", "zyna2386");
  45. System.out.println("Buvo sekmingai prisijungta prie DB!");
  46. }
  47. catch (SQLException sqle) {
  48. con = null;
  49. throw new Exception("Couldn't get connection!");
  50. }
  51. return con;
  52. }
  53.  
  54. public boolean isConnected() {
  55. if (con != null) {
  56. return true;
  57. }
  58. return false;
  59. }
  60.  
  61.  
  62. public void closeConnection() throws SQLException {
  63. con.close();
  64. }
  65. public void initPreparedStatements() {
  66. try {
  67.  
  68. this.stmt_insertLigonine = this.con.prepareStatement("INSERT INTO Ligonine VALUES(?, ?, ?)");
  69. this.stmt_selectLigonine = this.con.prepareStatement("SELECT * FROM Ligonine WHERE Nr = 1");
  70.  
  71. } catch (SQLException sqle) {
  72. System.out.println(sqle);
  73. }
  74. }
  75.  
  76. public ResultSet selectLigonine() throws SQLException {
  77. try{
  78. Statement stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  79. ResultSet result = stmt.executeQuery("SELECT * FROM Ligonine ORDER BY Nr");
  80. if (result.isBeforeFirst())
  81. return result;
  82. else
  83. return null;
  84. }
  85. catch(SQLException e){
  86. System.out.println(e);
  87. return null;
  88. }
  89. }
  90.  
  91. public void insertLigonine(int Nr, String adresas, String pavadinimas, String numeris) throws SQLException {
  92. try{
  93.  
  94. //this.stmt_insertLigonine.setInt(1, Nr);
  95. this.stmt_insertLigonine.setString(1, adresas);
  96. this.stmt_insertLigonine.setString(2, pavadinimas);
  97. this.stmt_insertLigonine.setString(3, numeris);
  98. this.stmt_insertLigonine.executeUpdate();
  99. }
  100. catch(SQLException e){
  101. System.out.println(e);
  102. }
  103.  
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement