1. Trang chủ
  2. » Công Nghệ Thông Tin

Ví dụ JDBC đơn giản

3 945 3
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Ví dụ JDBC đơn giản
Trường học University of Information Technology
Chuyên ngành Computer Science
Thể loại bài tập
Thành phố Ho Chi Minh City
Định dạng
Số trang 3
Dung lượng 36 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Ví dụ JDBC đơn giản

Trang 1

1 Ví dụ JDBC đơn giản

import 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()

} // class

2 Cập nhật thong tin trên ResultSet

import 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 {

Trang 2

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);

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ập

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:

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

Trang 3

- Cài đặt các chức năng tương tự cho bảng dữ liệu LOP.

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: (Trang 2)

TỪ KHÓA LIÊN QUAN

w