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 cv1;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- *
- * @author xsevcikj
- */
- public class Cv1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- Statement st = connect();
- select(st);
- count(st);
- Double cena = cenaKnihy("asgfg", st);
- System.out.println(cena);
- zlava("hahabebe", st);
- }
- public static Statement connect() throws ClassNotFoundException, SQLException{
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
- Statement st = con.createStatement();
- return st;
- }
- public static void select(Statement st) throws SQLException{
- ResultSet rs = st.executeQuery("SELECT * FROM APP.KNIHA");
- while (rs.next()){
- System.out.println(rs.getString(1));
- }
- }
- public static void count(Statement st) throws SQLException{
- ResultSet rs = st.executeQuery("SELECT count(*) FROM kniha");
- while (rs.next()){
- System.out.println("" + rs.getInt(1));
- }
- }
- public static Double cenaKnihy(String meno, Statement st) throws SQLException{
- ResultSet rs = st.executeQuery("SELECT cena FROM kniha WHERE nazov='"+meno+"'");
- double cena = -1.00;
- if (!rs.next()){
- System.out.println("Nemam taku knihu");
- return cena;
- }
- return rs.getDouble(1);
- }
- public static boolean pridajKnihu(String nazov, double cena, Statement st) throws SQLException{
- ResultSet rs = st.executeQuery("SELECT * FROM kniha WHERE nazov='"+nazov+"'");
- if (!rs.next()){
- return false;
- }
- st.executeUpdate("INSERT INTO kniha " + "VALUES ('"+nazov+"', '"+cena+"')");
- return true;
- }
- public static void zlava(String nazov, Statement st) throws SQLException{
- Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
- PreparedStatement ps = con.prepareStatement("UPDATE kniha SET cena = ? WHERE nazov = ?");
- ResultSet rs = st.executeQuery("SELECT cena FROM kniha WHERE nazov='"+nazov+"'");
- if (rs.next()){
- Double cena = rs.getDouble(1);
- cena = cena * 0.8;
- ps.setDouble(1, cena);
- ps.setString(2, nazov);
- ps.executeUpdate();
- ps.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement