Java Programming Language Chapter 10 Advanced Java Database
Java Programming Language Chapter 10 Advanced Java Database Programming Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 1
JDBC F ���������� 4 ������� § ������������������� Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 2
1. �������� �� F �� 2 ������� § ���� Driver Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"); § ����������� Connection con = Driver. Manager. get. Connection(URL); ���� String URL = "jdbc: odbc: Connect. Example"; Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"); Connection con =Driver. Manager. get. Connection(URL; ( Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 3
2. ������� F ����������� SQL F ����������� method “execute. Update()” F ����������� String URL = "jdbc: odbc: Connect. Example"; String SQL = "INSERT INTO Student(Id, Name, Faculty, Telphone, Address)"+ "VALUES(? , ? , ? )"; Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"); Connection con = Driver. Manager. get. Connection(URL); Prepared. Statement pstmt = con. prepare. Statement(SQL, Result. Set. TYPE_SCROLL_SENSITIVE, Result. Set. CONCUR_UPDATABLE); pstmt. set. String(1, Text. Id. get. Text()); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 pstmt. set. String(2, Text. Name. get. Text()); 4
3. ������ F ����������������� method “execute. Update()” F ������� , ����������������� method “execute. Query()” Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 5
Method F F F ���� Field �� Record pstmt. set. Int(int index, int x) pstmt. set. Boolean(int index, boolean x) pstmt. set. Byte(int index, byte x) pstmt. set. Short(int index, short x) pstmt. set. Long(int index, long x) pstmt. set. Float(int index, float x) pstmt. set. Double(int index, double x) pstmt. set. String(int index, String x) pstmt. set. Date(int index, Date x) pstmt. set. Time(int index, Time x) pstmt. set. Null(int index, Null) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 6
Method ������� field public Date get. Date(String) public double get. Double(String) public float get. Float(String) public int get. Int(String) public long get. Long(String) public String get. String(String) public boolean get. Boolean(String) public byte get. Byte(String) public short get. Short(String) public Time get. Time(String) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 7
Method �������� Record F rec. next() ����� F rec. previous() �������� Record ���� rec. last() �������� Record ������� rec. first() �������� Record ��� rec. after. Last() �������� Record ������� rec. before. First() �������� Record ��� Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All 8 F F //x �������� Record //x //x //x rights reserved. 0 -13 -222158 -6
Method F �������������� pointer �������� record ������� public boolean is. After. Last() F ������� pointer ����������� record ����� public boolean is. Before. First() F ������� pointer ����������� record ������ public boolean is. Last() F ������� pointer ����������� record ����� Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 9
����� class Result. Set F ������������ Result. Set. TYPE_SCROLL_SENSITIVE F ������������������������ method close() Result. Set. TYPE_SCROLL_INSENSITIVE F ������������ Result. Set. CONCUR_UPDATABLE F ������������ Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 10
Method ������� rec. update. Boolean(String Field, boolean x) rec. update. Byte(String Field, byte x( rec. update. Short(String Field, short x( rec. update. Int(String Field, int x( rec. update. Long(String Field, long x( rec. update. Float(String Field, float x( rec. update. Double(String Field, double x( rec. update. String(String Field, String x( rec. update. Date(String Field, Date x( rec. update. Time(String Field, Time x( Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 11
Method ������� Record F ���� �. ���������� public void delete. Row() F ������ �. ���������� public void insert. Row() F ������� �. ���������� public void update. Row() Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 12
4. ������� F ���������� F �������� method “close()” F ���� Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 13
ตวอยาง String URL = "jdbc: odbc: Connect. Example"; String SQL = "INSERT INTO Student(Id, Name, Faculty, Telphone, Address)"+ "VALUES(? , ? , ? )"; Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"); Connection con = Driver. Manager. get. Connection(URL); Prepared. Statement pstmt = con. prepare. Statement(SQL, Result. Set. TYPE_SCROLL_SENSITIVE, Result. Set. CONCUR_UPDATABLE); pstmt. set. String(1, Text. Id. get. Text()); pstmt. set. String(2, Text. Name. get. Text()); pstmt. execute. Update(); pstmt. close(); con. close(); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 14
����� F ���������� – �������� – ������ , �� ����� F ���������� Example. java Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 15
- Slides: 16