Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.*;
- import java.sql.ResultSet;
- public class DB {
- public Connection con = null;
- private PreparedStatement stmt_insertKomanda;
- private PreparedStatement stmt_updateKomanda;
- private PreparedStatement stmt_selectKomanda;
- private PreparedStatement stmt_deleteKomanda;
- private PreparedStatement stmt_insertZaidejas;
- private PreparedStatement stmt_updateZaidejas;
- private PreparedStatement stmt_selectZaidejas;
- private PreparedStatement stmt_deleteZaidejas;
- //--------------MANO----------------------
- private PreparedStatement stmt_insertLigonine;
- private PreparedStatement stmt_selectLigonine;
- public void loadDriver() throws Exception {
- try {
- Class.forName("org.postgresql.Driver");
- System.out.println("Driveris buvo sekmingai uzkrautas!");
- }
- catch (ClassNotFoundException cnfe) {
- throw new Exception("Unable to load the driver class!");
- }
- }
- public Connection getConnection() throws Exception {
- try {
- con = DriverManager.getConnection("jdbc:postgresql://pgsql2.mif/studentu", "zyna2386", "zyna2386");
- System.out.println("Buvo sekmingai prisijungta prie DB!");
- }
- catch (SQLException sqle) {
- con = null;
- throw new Exception("Couldn't get connection!");
- }
- return con;
- }
- public boolean isConnected() {
- if (con != null) {
- return true;
- }
- return false;
- }
- public void closeConnection() throws SQLException {
- con.close();
- }
- public void initPreparedStatements() {
- try {
- this.stmt_insertLigonine = this.con.prepareStatement("INSERT INTO Ligonine VALUES(?, ?, ?)");
- this.stmt_selectLigonine = this.con.prepareStatement("SELECT * FROM Ligonine WHERE Nr = 1");
- } catch (SQLException sqle) {
- System.out.println(sqle);
- }
- }
- public ResultSet selectLigonine() throws SQLException {
- try{
- Statement stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- ResultSet result = stmt.executeQuery("SELECT * FROM Ligonine ORDER BY Nr");
- if (result.isBeforeFirst())
- return result;
- else
- return null;
- }
- catch(SQLException e){
- System.out.println(e);
- return null;
- }
- }
- public void insertLigonine(int Nr, String adresas, String pavadinimas, String numeris) throws SQLException {
- try{
- //this.stmt_insertLigonine.setInt(1, Nr);
- this.stmt_insertLigonine.setString(1, adresas);
- this.stmt_insertLigonine.setString(2, pavadinimas);
- this.stmt_insertLigonine.setString(3, numeris);
- this.stmt_insertLigonine.executeUpdate();
- }
- catch(SQLException e){
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement