Presentation is loading. Please wait.

Presentation is loading. Please wait.

HW#4 Making Simple BBS Using JDBC

Similar presentations


Presentation on theme: "HW#4 Making Simple BBS Using JDBC"— Presentation transcript:

1 HW#4 Making Simple BBS Using JDBC
Jaesoo Yoo Chungbuk National University

2 Contents Introduction to JDBC HW Assignment Directions for HW
Example Main classes & methods JDBC driver installationnt program installation HW Assignment Directions for HW References

3 JDBC Introduction to JDBC Example Main classes & method
JDBC driver installation

4 Introduction to JDBC What is JDBC? “Java Database Connectivity”
Connector to access DB, when developing applications in JavaTM Platform JDBC API Driver DBMS Application SQL statement ResultSet

5 <An example code: Test.java>
import java.sql.*; class Test { public static void main(String[] args) { Connection con = null; Statement stmt = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection( "user", "passwd"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select name from product"); while (rs.next()) { String product = rs.getString(1); System.out.println(product); } } catch (Exception e) { e.printStackTrace(); } finally { if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (Exception e) { } <An example code: Test.java>

6 Class.forName(“oracle.jdbc.driver.OracleDriver”);
Main classes & method Loading JDBC driver Using Class.forName() Connecting to DB Using DriverManager.getConnection() is URL Class.forName(“oracle.jdbc.driver.OracleDriver”); Connection con = DriverManager.getConnection("jdbc:oracle:thin: @dbserver.chungbuk.ac.kr", "user", "passwd");

7 Main classes & method (cont’d)
Executing queries Using Statement class Using PreparedStatement class Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select name from product"); PreparedStatement pstmt = con.prepareStatement(“insert into product values(?, ?)”); pstmt.setString(1, “mp3”); pstmt.setInt(2, 150); pstmt.executeUpdate(); ※ Use executeUpdate() for insert, update, and delete

8 Main classes & method (cont’d)
Cursor operations Use methods of ResultSet class Ex) next(), getString(), etc. ResultSet rs = stmt.executeQuery("select name from product"); while (rs.next()) { String product = rs.getString(1); System.out.println(product); }

9 Main classes & method (cont’d)
Using ‘finally’ Before finishing code, connection should be closed try { con = DriverManager.getConnection( … ); stmt = con.createStatement(); } catch (Exception e) { e.printStackTrace(); } finally { if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (Exception e) {} }

10 Main classes & method (cont’d)
Executing Query within a Transaction try { con = DriverManager.getConnection( … ); con.setAutoCommit(false); stmt = con.createStatement(); stmt.executeQuery( … ); conn.commit(); } catch (SQLException e) { conn.rollback(); }

11 JDBC driver installation
JAVA SDK1.6 or 1.7 must be installed Download (ojdbc6.jar) Environment variable setting Add to the CLASSPATH environment variable the driver installation path Ex) If the path is c:\Oracle\jdbc\ojdbc6.jar

12 JDBC driver installation (cont’d)
Example file execution Compiling & running in a DOS command window import java.sql.*; class Test { public static void main(String[] args) { Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection( "user", "passwd"); System.out.println(“Connection created"); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) con.close(); } catch (Exception e) { } }

13 Eclipse Setting Working on Eclipse IDE
Add ojdbc6.jar to project build path Right click on JRE System Library  Build Path  Configure Build Path

14 Eclipse Setting (cont’d)
Working on Eclipse IDE Add External IDE  select ojdbc6.jar

15 Overview Homework Assignment Directions References

16 Overview Simple BBS manages user accounts
supports write/read/delete/show commands transaction control uses two tables ARTICLES and USERS in DB ARTICLES: contains users’ articles USERS: stores user account info. ARTICLES USERS NO: INT WRITER: VARCHAR2(10) COUNT: INT TITLE: VARCHAR2(30) CONTENT: VARCHAR2(500) USERID VARCHAR2(10) PASSWD VARCHAR2(10)

17 Homework #4 Initial screen user can login to BBS, or
create a new account by typing ‘new’

18 Homework #4 (cont’d) HW4-1) Managing user accounts
Create a new account check if there exists the same ID

19 Homework #4 (cont’d) HW4-1) Managing user accounts (cont’d)
Login to BBS If a user type a wrong ID or password three times, login is denied and the program exits.

20 Homework #4 (cont’d) HW4-2) Command: show(s)/quit(q) type ‘s’;
show the catalog of user articles in the order of article number (no) type ‘q’; BBS program exits

21 Homework #4 (cont’d) HW4-3) Command: read(r) & transaction control
type ‘r’; select article number(no); show the title and content of the selected article increase count by one after a user read this article

22 Homework #4 (cont’d) HW4-4) Command: write(w)
type ‘w’; enter title; input content; Note that entering content ends with the carriage return

23 Homework #4 (cont’d) HW4-5) Command: delete(d)
type ‘d’; select article number(no); Note that a user cannot delete the others’ articles

24 Homework #4 (cont’d) HW4-5) Command: delete(d) (cont’d)
After deletion, rearrange the article numbers

25 Submission Files to submit How to submit Due date
1. JAVA (*.java) files that implement the Simple BBS How to submit 조교) Due date December 21 (Sunday), 11:59 pm.

26 References JDBC documentation
JAVA Platform, Standard Edition 6 API Specification documentation


Download ppt "HW#4 Making Simple BBS Using JDBC"

Similar presentations


Ads by Google