Đề
bài: Tạo ra 2 ứng dụng độc lập. Ứng dụng đầu tiên
nhận câu lệnh cho bảng students có thuộc tính: name, id và age để thêm vào
bảng. Còn ứng dụng thứ hai thì in ra màn hình thông tin từ bảng students ở trên
Add
Student
Chức
năng chương trình:
-
Kết nối cơ sở dữ liệu
-
Thêm một trường vào cơ sở dữ
liệu
Cơ
chế:
-
Tạo kết nối đến Driver của hệ
quản trị cơ sở dữ liệu
-
Từ đó, thực thi câu lệnh xử lí
CSDL
-
Kiểm tra việc thêm có thành
công hay ko, nếu ko thì báo lỗi
Các lớp
và chức năng các lớp:
-
Lớp chứa hàm main, ko có thuộc
tính và phương thức gì khác
-
Tạo đối tượng Connection kết
nối đến Driver của hệ quản trị CSDL, nếu việc load driver không thành công thì
thoát chương trình
-
Tạo đối tượng Statement thực
thi câu lệnh xử lí CSDL (ở đây là Insert), sử dụng phương thức execute để thực
thi các lệnh không cần truy vấn, đối số
trả về là kiểu int, trả về 1 nếu thành công, và giá trị khác thì thất bại
Get
Students
Chức
năng chương trình:
-
Kết nối CSDL
-
Truy vấn
Cơ
chế:
-
Tạo kết nối đến Driver của hệ
quản trị cơ sở dữ liệu
-
Từ đó, thực thi câu lệnh truy
vấn CSDL
-
Xuất kết quả ra màn hình
Các
lớp và chức năng các lớp:
-
Tương tự như lớp AddStudent,
khi tạo Statement thì sử dụng phương thức excecuteQuery thay vì execute, kiểu
trả về là ResultSet
-
Từ ResultSet đã trả về, hiển
thị nó lên màn hình
Kết
quả ví dụ minh họa:
AddStudent
import java.sql.*;
import java.util.Scanner;
public class AddStudent {
Connection con;
Statement st;
int rs;
String name,age;
public AddStudent()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:SV");
st = con.createStatement();
}catch(Exception e){
}
}
public void Add()
{
try{
Scanner input = new Scanner(System.in);
System.out.println("Input Name: ");
name = input.nextLine();
System.out.println("Input Age: ");
age = input.nextLine();
String sql = "INSERT INTO Students([Name],[Age]) VALUES('"+name+"',"+age+")";
rs= st.executeUpdate(sql);
System.out.println("Insert Successfull: "+name+"\t"+age+"tuoi\n");
}catch (Exception e) {
System.out.println(e);
}
finally {
if(con!=null){
try{
st.close();
con.close();
}catch(Exception e){}
}
}
}
public static void main(String[] args) {
AddStudent O = new AddStudent();
O.Add();
}
}
GetStudent
import java.util.Scanner;
public class AddStudent {
Connection con;
Statement st;
int rs;
String name,age;
public AddStudent()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:SV");
st = con.createStatement();
}catch(Exception e){
}
}
public void Add()
{
try{
Scanner input = new Scanner(System.in);
System.out.println("Input Name: ");
name = input.nextLine();
System.out.println("Input Age: ");
age = input.nextLine();
String sql = "INSERT INTO Students([Name],[Age]) VALUES('"+name+"',"+age+")";
rs= st.executeUpdate(sql);
System.out.println("Insert Successfull: "+name+"\t"+age+"tuoi\n");
}catch (Exception e) {
System.out.println(e);
}
finally {
if(con!=null){
try{
st.close();
con.close();
}catch(Exception e){}
}
}
}
public static void main(String[] args) {
AddStudent O = new AddStudent();
O.Add();
}
}
import java.sql.*;
public class GetStudent {
Connection con;
Statement st;
public GetStudent(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:SV");
st = con.createStatement();
}catch(Exception e){
}
}
public void Get()
{
try{
String sql = "select * from Students";
ResultSet rs= st.executeQuery(sql);
while(rs.next()) {
String name=rs.getString("Name");
String age=rs.getString("Age");
System.out.println("Show Data: "+name+"\t"+age);
}
}catch (Exception e) {
System.out.println(e);
}
finally {
if(con!=null){
try{
st.close();
con.close();
}catch(Exception e){}
}
}
}
public static void main(String[] args) {
GetStudent O = new GetStudent();
O.Get();
}
}
public class GetStudent {
Connection con;
Statement st;
public GetStudent(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:SV");
st = con.createStatement();
}catch(Exception e){
}
}
public void Get()
{
try{
String sql = "select * from Students";
ResultSet rs= st.executeQuery(sql);
while(rs.next()) {
String name=rs.getString("Name");
String age=rs.getString("Age");
System.out.println("Show Data: "+name+"\t"+age);
}
}catch (Exception e) {
System.out.println(e);
}
finally {
if(con!=null){
try{
st.close();
con.close();
}catch(Exception e){}
}
}
}
public static void main(String[] args) {
GetStudent O = new GetStudent();
O.Get();
}
}







0 comments:
Post a Comment