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.
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Vector;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Mihael
- */
- public class DB {
- public java.sql.Connection conn;
- public Logger logger;
- public Logger logger_err;
- public DB(){
- try{
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public boolean isConnected(){
- if(this.conn!=null)
- return true;
- else
- return false;
- }
- public boolean disconnect() throws SQLException {
- boolean OK=false;
- try {
- if (conn != null) {
- conn.close();
- OK=true;
- }
- } catch (SQLException e) {
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- }
- finally{
- return OK;
- }
- }
- public void initConnection_MySQL() {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- /* DriverManager.getConnection("jdbc:mysql://"server":"port"/"dbname"?user="user"&password="password"&characterEncoding=Cp1250");
- */
- java.util.Properties props = new java.util.Properties();
- props.put("characterEncoding", "cp1250");
- props.put("characterSetResults", "cp1250");
- props.put("user", "root");
- props.put("password", "0000");
- this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mevb", props);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public boolean disconnect(Connection con) throws SQLException {
- boolean OK = false;
- try {
- if (con != null) {
- con.close();
- OK = true;
- }
- } catch (SQLException e) {
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- }
- return OK;
- }
- public boolean BrisanjePremaCijeni(int cijena) throws java.sql.SQLException{
- if(this.conn == null){return false;}
- try{
- Statement stm;
- String query ="DELETE FROM AUTOMOBIL WHERE CIJENA='"+cijena+"'";
- PreparedStatement stmt = this.conn.prepareStatement(query);
- stmt.executeUpdate();
- stmt.close();
- return true;
- }catch(SQLException e){
- return false;
- }
- }
- public boolean BrisanjePremaModelu(String model) throws java.sql.SQLException{
- if(this.conn == null){return false;}
- try{
- Statement stm;
- String query = "DELETE FROM AUTOMOBIL WHERE MODEL='"+model+"'";
- PreparedStatement stmt = this.conn.prepareStatement(query);
- stmt.executeUpdate();
- stmt.close();
- return true;
- }catch(SQLException e){
- return false;
- }
- }
- public boolean BrisanjePremaMarki(String marka) throws java.sql.SQLException{
- if(this.conn == null){return false;}
- try{
- Statement stm;
- String query = "DELETE FROM AUTOMOBIL WHERE MARKA='"+marka+"'";
- PreparedStatement stmt = this.conn.prepareStatement(query);
- stmt.executeUpdate();
- stmt.close();
- return true;
- }catch(SQLException e){
- return false;
- }
- }
- public Vector PrikazPremaCijeni(int cijena) throws java.sql.SQLException{
- Vector vec = new Vector();
- if(this.conn == null){
- return vec;
- }
- try{
- Statement stm = this.conn.createStatement();
- String query = "SELECT * FROM AUTOMOBIL WHERE CIJENA='"+cijena+"'";
- ResultSet rs = stm.executeQuery(query);
- while(rs.next()){
- Automobil auto = new Automobil();
- auto.setID(rs.getInt(1));
- auto.setNaziv(rs.getString(2));
- auto.setModel(rs.getString(3));
- auto.setCijena(rs.getInt(4));
- vec.addElement(auto);
- }
- stm.close();
- rs.close();
- query = null;
- return vec;
- }catch(SQLException e){
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- return vec;
- }
- }
- public Vector PrikazPremaModelu(String model) throws java.sql.SQLException{
- Vector vec = new Vector();
- if(this.conn == null){
- return vec;
- }
- try{
- Statement stm = this.conn.createStatement();
- String query = "SELECT * FROM AUTOMOBIL WHERE MODEL='"+model+"'";
- ResultSet rs = stm.executeQuery(query);
- while(rs.next()){
- Automobil auto = new Automobil();
- auto.setID(rs.getInt(1));
- auto.setNaziv(rs.getString(2));
- auto.setModel(rs.getString(3));
- auto.setCijena(rs.getInt(4));
- vec.addElement(auto);
- }
- stm.close();
- rs.close();
- query = null;
- return vec;
- }catch(SQLException e){
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- return vec;
- }
- }
- public Vector PrikazPremaNazivu(String naziv) throws java.sql.SQLException{
- Vector vec = new Vector();
- if(this.conn == null){
- return vec;
- }
- try{
- Statement stm = this.conn.createStatement();
- String query = "SELECT * FROM AUTOMOBIL WHERE MARKA='"+naziv+"'";
- ResultSet rs = stm.executeQuery(query);
- while(rs.next()){
- Automobil auto = new Automobil();
- auto.setID(rs.getInt(1));
- auto.setNaziv(rs.getString(2));
- auto.setModel(rs.getString(3));
- auto.setCijena(rs.getInt(4));
- vec.addElement(auto);
- }
- stm.close();
- rs.close();
- query = null;
- return vec;
- }catch(SQLException e){
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- return vec;
- }
- }
- public Vector PrikazAutomobila()throws java.sql.SQLException{
- Vector vec = new Vector();
- if (this.conn == null) {
- return vec;
- }
- try{
- Statement stm = this.conn.createStatement();
- String query = "SELECT * FROM AUTOMOBIL";
- ResultSet rs = stm.executeQuery(query);
- while(rs.next()){
- Automobil auto = new Automobil();
- auto.setID(rs.getInt(1));
- auto.setNaziv(rs.getString(2));
- auto.setModel(rs.getString(3));
- auto.setCijena(rs.getInt(4));
- vec.addElement(auto);
- }
- query = null;
- stm.close();
- rs.close();
- return vec;
- }catch(SQLException e){
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- return vec;
- }
- }
- public boolean UpisAuta(Automobil data){
- if(this.conn == null){
- return false;
- }
- try{
- Statement stm;
- String query = "INSERT INTO AUTOMOBIL VALUES(?,?,?,?)";
- PreparedStatement stmt = this.conn.prepareStatement(query);
- stmt.setInt(1,data.getID());
- stmt.setString(2, data.getNaziv());
- stmt.setString(3, data.getModel());
- stmt.setInt(4, data.getCijena());
- stmt.executeUpdate();
- stmt.close();
- return true;
- }catch(SQLException e){
- logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement