1、新建一个ACCESS数据库,新建一个表BookData,表结构如下:
-------------------------------------------------
字段名称 数据类型 字段大小
ISBN 文字 13
BookName 文字 50
Author 文字 30
Price 数字 长整数
--------------------------------------------------
2、在ODBC的用户DSN中新建一个数据源"myBook"。
3、源代码如下:
Code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
*
* @author Administrator
*/
public class MyBook extends JFrame{
Connection ConnObj;
Statement SQLStatement;
ResultSet RS;
//========================================
JPanel contentPanel;
Panel panel1;
Label L1,L2,L3,L4;
TextField T1,T2,T3,T4;
Button B1,B2,B3,B4;
public MyBook() {
contentPanel=(JPanel)this.getContentPane();
contentPanel.setLayout(null);
//=============================================
panel1=new Panel();
panel1.setBounds(0, 0, 290, 130);
panel1.setLayout(null);
panel1.setEnabled(false);
contentPanel.add(panel1);//panel1是加在contentPane内
//用来显示一条数据的组件是加在panel1内
L1=new Label("ISBN:");
L1.setBounds(20, 10, 50, 25);
panel1.add(L1);
L2=new Label("书名:");
L2.setBounds(20, 40, 50, 25);
panel1.add(L2);
L3=new Label("作者:");
L3.setBounds(20, 70, 50, 25);
panel1.add(L3);
L4=new Label("价格:");
L4.setBounds(20, 100, 50, 25);
panel1.add(L4);
T1=new TextField();
T1.setBounds(70, 10, 200, 25);
panel1.add(T1);
T2=new TextField();
T2.setBounds(70, 40, 200, 25);
panel1.add(T2);
T3=new TextField();
T3.setBounds(70, 70, 200, 25);
panel1.add(T3);
T4=new TextField();
T4.setBounds(70, 100, 200, 25);
panel1.add(T4);
//==========================================
B1=new Button("第一条");
B1.setBounds(new Rectangle(10, 150, 65, 20));
B1.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B1_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B1);
B2=new Button("上一条");
B2.setBounds(new Rectangle(80, 150, 65, 20));
B2.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B2_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B2);
B3=new Button("下一条");
B3.setBounds(new Rectangle(150, 150, 65, 20));
B3.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B3_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B3);
B4=new Button("最末条");
B4.setBounds(new Rectangle(220, 150, 65, 20));
B4.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B4_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B4);
//=========================================================
this.setBounds(200, 100, 310, 210);
this.setTitle("使用 Select 语句");
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
//=========================================================
//加载JDBC Driver、连接数据库、送SELECT语句、返回结果集
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//载入JDBC Driver
ConnObj=DriverManager.getConnection("jdbc:odbc:myBook");
//连接ODBC设置的myBook数据源,jdbc:subprotocol:Alias
SQLStatement=ConnObj.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//其后由此Statement对象执行SQL指令时,返回的会是可卷动且只读的ResultSet对象
RS=SQLStatement.executeQuery("select * from BookData");
//执行SELECT语句,会返回一个结果集
//查询myBook数据源的BookData数据表全部字段的记录
//必须先向下移到第一条:next(),因为一开始是指到第一条之前
if(RS.next())
{
//RS内至少有一条记录,才会返回true
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
else
JOptionPane.showMessageDialog(this, "myBook数据库内,无任何数据");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
void B1_mouseClicked(final MouseEvent e)
{
try
{
RS.first();
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
catch(SQLException ex){}
}
void B2_mouseClicked(final MouseEvent e)
{
try
{
if(RS.previous())
{
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
else
{
RS.first();
JOptionPane.showMessageDialog(B2,"已到第一条");
}
}
catch(SQLException ex){}
}
void B3_mouseClicked(final MouseEvent e)
{
try
{
if(RS.next())
{
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
else
{
RS.last();
JOptionPane.showMessageDialog(B3, "已到最末条");
}
}
catch(SQLException ex){}
}
void B4_mouseClicked(final MouseEvent e)
{
try
{
RS.last();
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
catch(SQLException ex){}
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
// TODO code application logic here
new MyBook();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
*
* @author Administrator
*/
public class MyBook extends JFrame{
Connection ConnObj;
Statement SQLStatement;
ResultSet RS;
//========================================
JPanel contentPanel;
Panel panel1;
Label L1,L2,L3,L4;
TextField T1,T2,T3,T4;
Button B1,B2,B3,B4;
public MyBook() {
contentPanel=(JPanel)this.getContentPane();
contentPanel.setLayout(null);
//=============================================
panel1=new Panel();
panel1.setBounds(0, 0, 290, 130);
panel1.setLayout(null);
panel1.setEnabled(false);
contentPanel.add(panel1);//panel1是加在contentPane内
//用来显示一条数据的组件是加在panel1内
L1=new Label("ISBN:");
L1.setBounds(20, 10, 50, 25);
panel1.add(L1);
L2=new Label("书名:");
L2.setBounds(20, 40, 50, 25);
panel1.add(L2);
L3=new Label("作者:");
L3.setBounds(20, 70, 50, 25);
panel1.add(L3);
L4=new Label("价格:");
L4.setBounds(20, 100, 50, 25);
panel1.add(L4);
T1=new TextField();
T1.setBounds(70, 10, 200, 25);
panel1.add(T1);
T2=new TextField();
T2.setBounds(70, 40, 200, 25);
panel1.add(T2);
T3=new TextField();
T3.setBounds(70, 70, 200, 25);
panel1.add(T3);
T4=new TextField();
T4.setBounds(70, 100, 200, 25);
panel1.add(T4);
//==========================================
B1=new Button("第一条");
B1.setBounds(new Rectangle(10, 150, 65, 20));
B1.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B1_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B1);
B2=new Button("上一条");
B2.setBounds(new Rectangle(80, 150, 65, 20));
B2.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B2_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B2);
B3=new Button("下一条");
B3.setBounds(new Rectangle(150, 150, 65, 20));
B3.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B3_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B3);
B4=new Button("最末条");
B4.setBounds(new Rectangle(220, 150, 65, 20));
B4.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(final MouseEvent e) {
B4_mouseClicked(e);//程序代码较多,写在另一个方法中
}
});
contentPanel.add(B4);
//=========================================================
this.setBounds(200, 100, 310, 210);
this.setTitle("使用 Select 语句");
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
//=========================================================
//加载JDBC Driver、连接数据库、送SELECT语句、返回结果集
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//载入JDBC Driver
ConnObj=DriverManager.getConnection("jdbc:odbc:myBook");
//连接ODBC设置的myBook数据源,jdbc:subprotocol:Alias
SQLStatement=ConnObj.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//其后由此Statement对象执行SQL指令时,返回的会是可卷动且只读的ResultSet对象
RS=SQLStatement.executeQuery("select * from BookData");
//执行SELECT语句,会返回一个结果集
//查询myBook数据源的BookData数据表全部字段的记录
//必须先向下移到第一条:next(),因为一开始是指到第一条之前
if(RS.next())
{
//RS内至少有一条记录,才会返回true
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
else
JOptionPane.showMessageDialog(this, "myBook数据库内,无任何数据");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
void B1_mouseClicked(final MouseEvent e)
{
try
{
RS.first();
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
catch(SQLException ex){}
}
void B2_mouseClicked(final MouseEvent e)
{
try
{
if(RS.previous())
{
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
else
{
RS.first();
JOptionPane.showMessageDialog(B2,"已到第一条");
}
}
catch(SQLException ex){}
}
void B3_mouseClicked(final MouseEvent e)
{
try
{
if(RS.next())
{
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
else
{
RS.last();
JOptionPane.showMessageDialog(B3, "已到最末条");
}
}
catch(SQLException ex){}
}
void B4_mouseClicked(final MouseEvent e)
{
try
{
RS.last();
T1.setText(RS.getString("ISBN"));
T2.setText(RS.getString("BookName"));
T3.setText(RS.getString("Author"));
T4.setText(String.valueOf(RS.getInt("Price")));
}
catch(SQLException ex){}
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
// TODO code application logic here
new MyBook();
}
}