我正在尝试创建一个程序来访问MS Access中的数据库,我认为它没有正确访问它,但我也遇到了applet工作的问题。当我运行该程序时,小程序出现,但它没有任何内容。我已创建Access数据库,保存它,并通过控制面板中的ODBC添加它。 这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
public class GUI extends Database{
private JFrame JF;
private JLabel JLFName;
private JLabel JLLName;
private JLabel JLAge;
private JTextField JTFName;
private JTextField JTLName;
private JTextField JTAge;
private JButton NxtBtn = new JButton("Next");
private JButton PrvBtn = new JButton("Previous");
private JButton FstBtn = new JButton("First");
private JButton LstBtn = new JButton("Last");
private JButton UpdBtn = new JButton("Update");
private JButton DelBtn = new JButton("Delete");
private JButton AddBtn = new JButton("Add");
private JButton SveBtn = new JButton("Save");
public GUI(){
Frame();
BtnAction();
}
public void Frame(){
JF = new JFrame();
JF.setVisible(true);
JF.setSize(600, 400);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLFName = new JLabel("First Name");
JLLName = new JLabel("Last Name");
JLAge = new JLabel("Age Name");
JTFName = new JTextField(10);
JTLName = new JTextField(10);
JTAge = new JTextField(10);
JPanel JP = new JPanel();
JP.add(JLFName);
JP.add(JTFName);
JP.add(JLLName);
JP.add(JTLName);
JP.add(JLAge);
JP.add(JTAge);
JP.add(NxtBtn);
JP.add(PrvBtn);
JP.add(FstBtn);
JP.add(LstBtn);
JP.add(UpdBtn);
JP.add(DelBtn);
JP.add(AddBtn);
JP.add(SveBtn);
JF.add(JP);
try{
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex){
}
}
public void BtnAction(){
NxtBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
if(RS.next()){
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
else{
RS.previous();
JOptionPane.showMessageDialog
(null, "No more records");
}
}
catch(Exception ex){
}
}
});
PrvBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
if(RS.previous()){
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
else{
RS.next();
JOptionPane.showMessageDialog
(null, "No more records");
}
}
catch(Exception ex){
}
}
});
LstBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
RS.last();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex){
}
}
});
FstBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
RS.first();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex){
}
}
});
UpdBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String FName = JTFName.getText();
String LName = JTLName.getText();
String Age = JTAge.getText();
try{
RS.updateString("FName", FName);
RS.updateString("LName", LName);
RS.updateString("Age", Age);
RS.updateRow();
JOptionPane.showMessageDialog
(null, "Record Updated");
}
catch(Exception ex){
}
}
});
DelBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
RS.deleteRow();
St.close();
RS.close();
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex){
}
}
});
AddBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JTFName.setText(" ");
JTLName.setText(" ");
JTAge.setText(" ");
}
});
SveBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String FName = JTFName.getText();
String LName = JTLName.getText();
String Age = JTAge.getText();
try{
RS.moveToInsertRow();
RS.updateString("FName", FName);
RS.updateString("LName", LName);
RS.updateString("Age", Age);
RS.insertRow();
St.close();
RS.close();
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
RS.next();
JTFName.setText(RS.getString("FName"));
JTLName.setText(RS.getString("LName"));
JTAge.setText(RS.getString("Age"));
}
catch(Exception ex){
}
}
});
}
}
import java.sql.*;
public class Database{
Connection Con;
Statement St;
ResultSet RS;
public Database(){
Connect();
}
public void Connect(){
try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String Database = "jdbc:odbc:FinalDB";
Con = DriverManager.getConnection(Database);
St = Con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from MyTable";
RS = St.executeQuery(sql);
}
catch(Exception ex){
}
}
public static void main(String[] args){
new Database();
new GUI();
}
}
答案 0 :(得分:0)
你必须写JF.setVisible(true);初始化所有组件并将其添加到框架后的指令。那种方法吸引了他们。