APJII lab 02

4 60 0
APJII lab 02

Đang tải... (xem toàn văn)

Thông tin tài liệu

AdvJ-Lab1-Thread DBSJ-Lab2-Store procedures, Scrollable Resultset and Rowsets Advanced Programming in Java - II Lab Guide for Session 2: Module 2: Store procedures Module 3: Scrollable Resultset and Rowsets Session Objectives In this session, you will be practicing with  Store procedure  Scrollable, updatable “resultset”  Batch updates and transactions  Rowset  JDBCRowset  CachedRowset Part – Getting started (30 minutes) Create a jdbc application using odbc-jdbc driver(15 minutes) The following application will list id and name of all categories in database Northwind Create a procedure named sp_all_categories create proc sp_all_categories as select CategoryID, CategoryName from Categories Scan the code first, type the code, compile, run and observe the result import import import import import import java.sql.CallableStatement; java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.util.logging.Level; java.util.logging.Logger; public class DemoCallable { private static String url="jdbc:sqlserver:"+ "//localhost\\SQLEXPRESS;databaseName=Northwind"; public static void main(String[] args) { try { //1 register driver This can be omitted Class.forName( © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page / AdvJ-Lab1-Thread DBSJ-Lab2-Store procedures, Scrollable Resultset and Rowsets "com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2 get conn Connection = DriverManager.getConnection( url,"sa","fat123456"); //3 execute CallableStatement statement = con.prepareCall("{call sp_all_categories}"); ResultSet rs =statement.executeQuery(); while (rs.next()) { System.out.println(rs.getString("CategoryID")+ "-"+rs.getString("CategoryName")); } con.close(); }catch (Exception ex) { Logger.getLogger(DemoStatement.class.getName()) log(Level.SEVERE, null, ex); } } } Create a jdbc application using jdbc driver type IV(15 minutes) The following application will insert new record to Categories table in database Northwind Scan the code first, type the code, compile, run and observe the result import import import import import import java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.Statement; java.util.logging.Level; java.util.logging.Logger; public class UpdatableResultSet { private static String url = "jdbc:sqlserver:" + "//localhost\\SQLEXPRESS;databaseName=Northwind"; public static void main(String[] args) { try { //1 register driver Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2 get conn Connection = DriverManager.getConnection( url, "sa", "fat123456"); //3 execute © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page / AdvJ-Lab1-Thread DBSJ-Lab2-Store procedures, Scrollable Resultset and Rowsets Statement statement = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String query="select CategoryID, CategoryName "+ "from Categories"; ResultSet rs = statement.executeQuery(query); rs.moveToInsertRow(); rs.updateString("CategoryName", "Beer"); rs.insertRow(); System.out.println("Inserted"); con.close(); } catch (Exception ex) { Logger.getLogger(UpdatableResultSet.class.getName()) log(Level.SEVERE, null, ex); } } } Part – Workshops (30 minutes) • • Quickly look at Module 2’s workshops for reviewing basic steps for work CallableStatement, Procedure Stored, PrepairedStatement and updatable resultset Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module carefully Discuss with your class-mates and your instructor if needed Part – Do it your self Write a Java application StudentDetails that will traverse records from a student table by using disconnected RowSet The output of the program is as shown as below © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page / AdvJ-Lab1-Thread DBSJ-Lab2-Store procedures, Scrollable Resultset and Rowsets Rewrite Exercise by using connected RowSet © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page /

Ngày đăng: 27/10/2019, 09:26

Mục lục

    Advanced Programming in Java - II

    Part 1 – Getting started (30 minutes)

    Part 3 – Lab Assignment (60 minutes)

    Part 4 – Do it your self

Tài liệu cùng người dùng

Tài liệu liên quan