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 jdbc;
- 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.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class JDBCTest1 {
- public static void main(String[] args) {
- dodajProizvod(543,19985678,"SW",555.333,1,12,"hue","helo be humble");
- nadjiProizvode();
- }
- private static void nadjiProizvode() {
- String url = "jdbc:derby://localhost:1527/sample";
- String upit = "select * from product";
- try (Connection conn = DriverManager.getConnection(url,
- "app", "app");
- Statement stat = conn.createStatement();
- ResultSet rs = stat.executeQuery(upit)) {
- while (rs.next()) {
- int prId = rs.getInt(1);
- int manId = rs.getInt(2);
- String desc = rs.getString("DESCRIPTION");
- double cost = rs.getDouble(4);
- System.out.println("ID: " + prId
- + "\t\t Naziv: " + desc
- + "\t\t Cena: " + cost);
- }
- } catch (SQLException ex) {
- Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE,
- null, ex);
- }
- }
- private static void dodajProizvod(int prId, int manId, String prodCode, double cost, int quan, double markup, String ava, String desc) {
- String url = "jdbc:derby://localhost:1527/sample";
- String upit = "insert into product values(?,?,?,?,?,?,?,?)";
- try (Connection conn = DriverManager.getConnection(url,
- "app", "app");
- PreparedStatement stat = conn.prepareStatement(upit)) {
- conn.setAutoCommit(false);
- stat.setInt(1, prId);
- stat.setInt(2, manId);
- stat.setString(3, prodCode);
- stat.setDouble(4, cost);
- stat.setInt(5, quan);
- stat.setDouble(6, markup);
- stat.setString(7, ava);
- stat.setString(8, desc);
- int count = stat.executeUpdate();//vraca broj updatovanih redova
- if (count > 0) {
- conn.commit();
- } else {
- conn.rollback();
- }
- } catch (SQLException ex) {
- Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE,
- null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement