Ví dụ JDBC đơn giản

3 942 3
Ví dụ JDBC đơn giản

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

Thông tin tài liệu

Ví dụ JDBC đơn giản

1. Ví dụ JDBC đơn giảnimport java.sql.*;class JDBCQuery { public static void main(String args[]) { try { // Load the database driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Get a connection to the database Connection conn = DriverManager.getConnection("jdbc:odbc:_tên_DSN"); // Get a statement from the connection Statement stmt = conn.createStatement(); // Execute the query ResultSet rs = stmt.executeQuery("SELECT * FROM Cust"); // Loop through the result set while (rs.next()) System.out.println(rs.getString(1)); // Close the result set, statement and the connection rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { System.out.println("SQL Exception:"); // Loop through the SQL Exceptions while (se != null) { System.out.println("State : " + se.getSQLState()); System.out.println("Message: " + se.getMessage()); System.out.println("Error : " + se.getErrorCode()); se = se.getNextException(); } } catch (Exception e) { System.out.println(e); } } // main()} // class2. Cập nhật thong tin trên ResultSetimport java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; public class DemoUpdatableResultSet { public static Connection getConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:databaseName"; String username = "name"; String password = "password"; Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, password); return conn; } public static void main(String[] args) { ResultSet rs = null; Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); String query = "select id, name, age from employees where age > ?"; pstmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); pstmt.setInt(1, 20); // set input values rs = pstmt.executeQuery(); // create an updatable ResultSet // update a column value in the current row. rs.absolute(2); // moves the cursor to the 2nd row of rs rs.updateString("name", "newName"); // updates the 'name' column // of row 2 to newName rs.updateRow(); // updates the row in the data source // insert column values into the insert row. rs.moveToInsertRow(); // moves cursor to the insert row rs.updateInt(1, 1234); // 1st column id=1234 rs.updateString(2, "newName"); // updates the 2nd column rs.updateInt(3, 99); // updates the 3rd column to 99 rs.insertRow(); rs.moveToCurrentRow(); } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }}3. Bài tậpCho CSDL đơn giản gồm 3 bảng dữ liệu: SINHVIEN, LOP, SINHVIEN_LOPSV tự cài đặt một bài tập làm việc với CSDL với các chức năng cơ bản:- Liệt kê danh sách sinh viên:o Cột mã số SV là một hyperlink cho phép hiển thị thông tin của SV tương ứng lên 1 form cho phép người dùng sửa thông tin SV đó o Bổ sung thêm một cột với các checkbox cho phép người dùng chọn để xóa một hoặc nhiều SV trong danh sách- Thêm một SV vào CSDL- Cài đặt các chức năng tương tự cho bảng dữ liệu LOP. . 1. Ví dụ JDBC đơn giảnimport java.sql.*;class JDBCQuery { public static void main(String args[]) {. Class.forName("sun .jdbc. odbc.JdbcOdbcDriver"); // Get a connection to the database Connection conn = DriverManager.getConnection(" ;jdbc: odbc:_tên_DSN");

Ngày đăng: 18/09/2012, 10:12

Hình ảnh liên quan

Cho CSDL đơn giản gồm 3 bảng dữ liệu: SINHVIEN, LOP, SINHVIEN_LOP SV tự cài đặt một bài tập làm việc với CSDL với các chức năng cơ bản: -Liệt kê danh sách sinh viên: - Ví dụ JDBC đơn giản

ho.

CSDL đơn giản gồm 3 bảng dữ liệu: SINHVIEN, LOP, SINHVIEN_LOP SV tự cài đặt một bài tập làm việc với CSDL với các chức năng cơ bản: -Liệt kê danh sách sinh viên: Xem tại trang 2 của tài liệu.

Từ khóa liên quan

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

Tài liệu liên quan