mini tutorial derby.txt

(3 KB) Pobierz
----------------------------------------
    public String framework = "embedded";
    public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    public String protocol = "jdbc:derby:";


--------------------------------------------
1.//�adownie sterownika
              Class.forName(driver).newInstance();
---------------------------------

2.//polaczenie z baza
              Connection conn = null;
---------------------------------------            
3. //ustawienia haslo uzytkownik
              Properties props = new Properties();
              props.put("user", "user1");
              props.put("password", "user1");
------------------------
4.tworzy polaczenie i baze
            conn = DriverManager.getConnection(protocol +"derbyDB;create=true", props);
            
	   conn.setAutoCommit(false);	
-----------------------------
5. Tworzy "wypowiedz"
		 Statement s = conn.createStatement();
-----------------------------------------------------
6. Tworzymy sobie tabelke
  s.execute("create table derbyDB(num int, addr varchar(40))");

----------------------------------------------------------
7. wrzucamy sobie dane 
 s.execute("insert into derbyDB values (1956,'Webster St.')");
 s.execute("insert into derbyDB values (1910,'Union St.')");
------------------------------------------------
8 uaktualniamy sobie 
            s.execute( "update derbyDB set num=180, addr='Grand Ave.' where num=1956");
----------------------------------------------------------------
9 Select danych 

  ResultSet rs = s.executeQuery("SELECT num, addr FROM derbyDB ORDER BY num");

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }
---------------------------------------------------------
10 zamyaknie polacznia
            rs.close();
            s.close();
-------------------

try{
wszystko

}

        catch (Throwable e)
        {
            System.out.println("exception thrown:");

            if (e instanceof SQLException)
            {
                printSQLError((SQLException) e);
            }
            else
            {
                e.printStackTrace();
            }
        }

        System.out.println("SimpleApp finished");
    }

    static void printSQLError(SQLException e)
    {
        while (e != null)
        {
            System.out.println(e.toString());
            e = e.getNextException();
        }
    }
-----------------------------------------------------------------------------

zapytania:

      PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)");

      psInsert.setString(1, args[0]);
      psInsert.setString(2, args[1]);

      psInsert.executeUpdate();




     Statement stmt2 = conn.createStatement();
      ResultSet rs = stmt2.executeQuery("select * from Employee");
      int num = 0;
      while (rs.next()) {
        System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2));
      }
      rs.close();


---------------------------------------------------------------------------------------


















Zgłoś jeśli naruszono regulamin