Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package db;
- import java.sql.*;
- /**
- *
- * @author vali
- */
- public class JavaBean {
- String error;
- Connection con;
- public JavaBean() {
- }
- public void connect() throws ClassNotFoundException, SQLException, Exception {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca?useSSL=false", "root", "qwerty97");
- } catch (ClassNotFoundException cnfe) {
- error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
- throw new ClassNotFoundException(error);
- } catch (SQLException cnfe) {
- error = "SQLException: Nu se poate conecta la baza de date.";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
- throw new Exception(error);
- }
- } // connect()
- public void connect(String bd) throws ClassNotFoundException, SQLException, Exception {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + bd, "root", "qwerty97");
- } catch (ClassNotFoundException cnfe) {
- error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
- throw new ClassNotFoundException(error);
- } catch (SQLException cnfe) {
- error = "SQLException: Nu se poate conecta la baza de date.";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
- throw new Exception(error);
- }
- } // connect(String bd)
- public void connect(String bd, String ip) throws ClassNotFoundException, SQLException, Exception {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/" + bd, "root", "qwerty97");
- } catch (ClassNotFoundException cnfe) {
- error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
- throw new ClassNotFoundException(error);
- } catch (SQLException cnfe) {
- error = "SQLException: Nu se poate conecta la baza de date.";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
- throw new Exception(error);
- }
- } // connect(String bd, String ip)
- public void disconnect() throws SQLException {
- try {
- if (con != null) {
- con.close();
- }
- } catch (SQLException sqle) {
- error = ("SQLException: Nu se poate inchide conexiunea la baza de date.");
- throw new SQLException(error);
- }
- } // disconnect()
- public void adaugaCarte(String Titlu, String ISBN, String Editura)
- throws SQLException, Exception {
- if (con != null) {
- try {
- // create a prepared SQL statement
- Statement stmt;
- stmt = con.createStatement();
- stmt.executeUpdate("insert into carti(Titlu, ISBN, Editura) values('" + Titlu + "' , '" + ISBN + "', '" + Editura + "');");
- } catch (SQLException sqle) {
- error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
- throw new SQLException(error);
- }
- } else {
- error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
- throw new Exception(error);
- }
- } // end of adaugaPacient()
- public void adaugaAutor(String Nume, String Prenume, String Nationalitate)
- throws SQLException, Exception {
- if (con != null) {
- try {
- // create a prepared SQL statement
- Statement stmt;
- stmt = con.createStatement();
- stmt.executeUpdate("insert into autori(Nume, Prenume, Nationalitate) values('" + Nume + "' , '" + Prenume + "', '" + Nationalitate + "');");
- } catch (SQLException sqle) {
- error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
- throw new SQLException(error);
- }
- } else {
- error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
- throw new Exception(error);
- }
- } // end of adaugaMedic()
- public void adaugaOpera(int idCarte, int idAutor, String NumeOpera, String GenOpera, String NrPagini)
- throws SQLException, Exception {
- if (con != null) {
- try {
- // create a prepared SQL statement
- Statement stmt;
- stmt = con.createStatement();
- stmt.executeUpdate("insert into opera(idCarte, idAutor, Nume, Gen, NrPagini) values('" + idCarte + "' , '" + idAutor + "', '" + NumeOpera + "', '" + GenOpera + "', '" + NrPagini+ "');");
- // se poate modifica valoarea datei astfel incat sa se ia data curenta a sistemului:
- // stmt.executeUpdate("insert into consultatie(idpacient, idmedic, DataConsultatie, Diagnostic, Medicament) values('" + idpacient + "' , '" + idmedic + "', CURDATE(), '" + Diagnostic + "', '" + Medicament + "');");
- } catch (SQLException sqle) {
- error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
- throw new SQLException(error);
- }
- } else {
- error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
- throw new Exception(error);
- }
- } // end of adaugaConsultatie()
- public ResultSet vedeTabela(String tabel) throws SQLException, Exception {
- ResultSet rs = null;
- try {
- String queryString = ("select * from `biblioteca`.`" + tabel + "`;");
- Statement stmt = con.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY*/);
- rs = stmt.executeQuery(queryString);
- } catch (SQLException sqle) {
- error = "SQLException: Interogarea nu a fost posibila. qweqwfwefwef";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "A aparut o exceptie in timp ce se extrageau datele.";
- throw new Exception(error);
- }
- return rs;
- } // vedeTabela()
- public ResultSet vedeOpera() throws SQLException, Exception {
- ResultSet rs = null;
- try {
- String queryString = ("select a.Titlu TitluCarte, a.ISBN , a.Editura EdituraCarte, b.Nume NumeAutor, b.Prenume PrenumeAutor, b.Nationalitate, c.idopera, c.idAutor, c.idCarte, c.Nume NumeOpera, c.Gen GenOpera, c.NrPagini from carti a, autori b, opera c where a.idCarte = c.idCarte and b.idAutor = c.idAutor;");
- Statement stmt = con.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY*/);
- rs = stmt.executeQuery(queryString);
- } catch (SQLException sqle) {
- error = "SQLException: Interogarea nu a fost posibila. dgdg";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "A aparut o exceptie in timp ce se extrageau datele.";
- throw new Exception(error);
- }
- return rs;
- } // vedeConsultatie()
- public void stergeDateTabela(String[] primaryKeys, String tabela, String dupaID) throws SQLException, Exception {
- if (con != null) {
- try {
- // create a prepared SQL statement
- long aux;
- PreparedStatement delete;
- delete = con.prepareStatement("DELETE FROM " + tabela + " WHERE " + dupaID + "=?;");
- for (int i = 0; i < primaryKeys.length; i++) {
- aux = java.lang.Long.parseLong(primaryKeys[i]);
- delete.setLong(1, aux);
- delete.execute();
- }
- } catch (SQLException sqle) {
- error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "A aparut o exceptie in timp ce erau sterse inregistrarile.";
- throw new Exception(error);
- }
- } else {
- error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
- throw new Exception(error);
- }
- } // end of stergeDateTabela()
- public void stergeTabela(String tabela) throws SQLException, Exception {
- if (con != null) {
- try {
- // create a prepared SQL statement
- Statement stmt;
- stmt = con.createStatement();
- stmt.executeUpdate("delete from " + tabela + ";");
- } catch (SQLException sqle) {
- error = "ExceptieSQL: Stergere nereusita; este posibil sa existe duplicate.";
- throw new SQLException(error);
- }
- } else {
- error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
- throw new Exception(error);
- }
- } // end of stergeTabela()
- public void modificaTabela(String tabela, String IDTabela, int ID, String[] campuri, String[] valori) throws SQLException, Exception {
- String update = "update " + tabela + " set ";
- String temp = "";
- if (con != null) {
- try {
- for (int i = 0; i < campuri.length; i++) {
- if (i != (campuri.length - 1)) {
- temp = temp + campuri[i] + "='" + valori[i] + "', ";
- } else {
- temp = temp + campuri[i] + "='" + valori[i] + "' where " + IDTabela + " = '" + ID + "';";
- }
- }
- update = update + temp;
- // create a prepared SQL statement
- Statement stmt;
- stmt = con.createStatement();
- stmt.executeUpdate(update);
- } catch (SQLException sqle) {
- error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
- throw new SQLException(error);
- }
- } else {
- error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
- throw new Exception(error);
- }
- } // end of modificaTabela()
- public ResultSet intoarceLinie(String tabela, int ID) throws SQLException, Exception {
- ResultSet rs = null;
- try {
- // Execute query
- String queryString = ("SELECT * FROM " + tabela + " where idpacient=" + ID + ";");
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery(queryString); //sql exception
- } catch (SQLException sqle) {
- error = "SQLException: Interogarea nu a fost posibila.";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "A aparut o exceptie in timp ce se extrageau datele.";
- throw new Exception(error);
- }
- return rs;
- } // end of intoarceLinie()
- public ResultSet intoarceLinieDupaId(String tabela, String denumireId, int ID) throws SQLException, Exception {
- ResultSet rs = null;
- try {
- // Execute query
- String queryString = ("SELECT * FROM " + tabela + " where " + denumireId + "=" + ID + ";");
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery(queryString); //sql exception
- } catch (SQLException sqle) {
- error = "SQLException: Interogarea nu a fost posibila. 453";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "A aparut o exceptie in timp ce se extrageau datele.";
- throw new Exception(error);
- }
- return rs;
- } // end of intoarceLinieDupaId()
- public ResultSet intoarceOperaId(int ID) throws SQLException, Exception {
- ResultSet rs = null;
- try {
- // Execute query
- String queryString = ("SELECT a.Titlu , a.ISBN , a.Editura EdituraCarte, b.Nume NumeAutor, b.Prenume PrenumeAutor, b.Nationalitate, c.idopera, c.idAutor, c.idCarte, c.Nume NumeOpera, c.Gen GenOpera, c.NrPagini from carti a, autori b, opera c where a.idCarte = c.idCarte and b.idAutor = c.idAutor and idopera = '" + ID + "';");
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs = stmt.executeQuery(queryString); //sql exception
- } catch (SQLException sqle) {
- error = "SQLException: Interogarea nu a fost posibila. rg";
- throw new SQLException(error);
- } catch (Exception e) {
- error = "A aparut o exceptie in timp ce se extrageau datele.";
- throw new Exception(error);
- }
- return rs;
- } // end of intoarceLinieDupaId()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement