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 javaapplication1;
- import java.sql.*;
- /**
- *
- * @author xpodluckyf
- */
- public class JavaApplication1 {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "org.apache.derby.jdbc.ClientDrive";
- static final String DB_URL = "jdbc:derby://localhost:1527/sample";
- // Database credentials
- static final String USER = "app";
- static final String PASS = "app";
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- //STEP 3: Open a connection
- Statement stmt = null;
- Statement stmt1 = null;
- Connection conn = null;
- try{ Class.forName("org.apache.derby.jdbc.ClientDriver");
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- //STEP 4: Execute a query
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT * FROM KNIHA";
- ResultSet rs = stmt.executeQuery(sql);
- stmt1 = conn.createStatement();
- String sql2 = "UPDATE KNIHA set cena=cena*0.9 where cena > 200";
- stmt1.executeUpdate(sql2);
- //STEP 5: Extract data from result set
- while(rs.next()){
- //Retrieve by column name
- String nazov = rs.getString("nazov_knihy");
- int cena = rs.getInt("cena");
- //Display values
- System.out.print("Nazov: " + nazov);
- System.out.print(", Cena: " + cena);
- }
- System.out.println();
- //STEP 6: Clean-up environment
- rs.close();
- stmt.close();
- conn.close();
- }catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- e.printStackTrace();
- }finally{
- //finally block used to close resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Goodbye!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement