gpt4 book ai didi

java - 使用 AWT 按钮浏览并显示 'textfield' 中的每条数据库记录

转载 作者:行者123 更新时间:2023-11-29 05:41:39 25 4
gpt4 key购买 nike

如何使用 AWT 按钮浏览并显示“文本字段”中的每条数据库记录?

我正在尝试制作一个 Java 表单来浏览数据库记录,这样当按下“下一步”按钮时,每次后续点击都会将下一条数据库记录提取到相应的文本字段中。这段代码的问题是无论我按下下一步按钮多少次,文本字段中只显示第一条记录。

我在几个线程上读到,if(rs.next()) 条件在这种情况下将 起作用。对此的正确解决方法是什么?为什么?

PS:我已经在 Problem code beginsProblem code ends 中标记了有问题的区域。

import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class ManageEmployees implements ActionListener
{
Connection con;

Statement st;

ResultSet rs;

Frame f;

Choice employeeID;

Button savenewemployeeButton, updateemployeeButton, returndashboardButton, nextButton/*, previousButton, lastButton, firstButton*/;

Label nameLabel, usernameLabel, addressLabel, contactnumberLabel, passwordLabel, confirmpasswordLabel, selectemployeeLabel;

TextField nameTextField, usernameTextField, addressTextField, passwordTextField, confirmpasswordTextField;

ManageEmployees()
{
dbconnect();
initframe();
}

public void dbconnect()
{
try{

Class.forName("com.mysql.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localhost:8889/InventoryManagement","root","root");

st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql="select * from SalesMen";

rs=st.executeQuery(sql);

}
catch(Exception ce)
{
ce.printStackTrace();
}
}

public void initframe()
{
f=new Frame("Manage Employees");

selectemployeeLabel=new Label("Select Employee Type:");
nameLabel=new Label("Employee Name:");
addressLabel=new Label("Address:");
usernameLabel=new Label("User Name:");
passwordLabel=new Label("Password:");
confirmpasswordLabel=new Label("Confirm Password:");

employeeID=new Choice();

employeeID.add("Sales Person");
employeeID.add("Sales Manager");
employeeID.add("Inventory Manager");
employeeID.add("Administrator");

savenewemployeeButton=new Button("Save New Employee details");
savenewemployeeButton.addActionListener(this);
returndashboardButton=new Button("Return to Dashboard");
returndashboardButton.addActionListener(this);

nextButton=new Button("Next");
nextButton.addActionListener(this);

nameTextField=new TextField(30);
addressTextField=new TextField(50);
usernameTextField=new TextField(20);
passwordTextField=new TextField(15);
confirmpasswordTextField=new TextField(15);

f.setLayout(new GridLayout(10,2,0,2));

f.add(selectemployeeLabel);
f.add(employeeID);

f.add(nameLabel);
f.add(nameTextField);

f.add(addressLabel);
f.add(addressTextField);

f.add(usernameLabel);
f.add(usernameTextField);

f.add(passwordLabel);
f.add(passwordTextField);

f.add(confirmpasswordLabel);
f.add(confirmpasswordTextField);

f.add(returndashboardButton);
f.add(savenewemployeeButton);
f.add(nextButton);

passwordTextField.setEchoChar('*');
confirmpasswordTextField.setEchoChar('*');

f.setSize(500,400);
f.setVisible(true);
f.setResizable(false);


f.addWindowListener(new WindowAdapter()
{

public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}

public void actionPerformed(ActionEvent e)
{

// Return to dashboard on button click

if(e.getSource()==returndashboardButton)
{
f.dispose();
new Dashboard();
}

// ---------------------------------------- (Begin) Save new employee information ------------------------------------------------------------//

// Save new Sales Person information

String salesmen=employeeID.getSelectedItem();


if(salesmen=="Sales Person" && e.getSource()==savenewemployeeButton)
{

try
{
String fullname1=nameTextField.getText();
String address1=usernameTextField.getText();
String username1=addressTextField.getText();
String password1=passwordTextField.getText();

String sql="insert into SalesMen values('"+fullname1+"','"+address1+"','"+username1+"','"+password1+"')";
st.executeUpdate(sql);

JOptionPane.showMessageDialog(null,"New Sales Man's details saved!");

}

catch(Exception ce)
{
ce.printStackTrace();
}

}

// Save new Sales Manager information

String salesmanager=employeeID.getSelectedItem();

if(salesmanager=="Sales Manager" && e.getSource()==savenewemployeeButton)
{
try
{

String fullname1=nameTextField.getText();
String address1=usernameTextField.getText();
String username1=addressTextField.getText();
String password1=passwordTextField.getText();

String sql="insert into SalesManagers values('"+fullname1+"','"+address1+"','"+username1+"','"+password1+"')";
st.executeUpdate(sql);

JOptionPane.showMessageDialog(null,"New Sales Manager's details saved!");

}
catch(Exception ce)
{
ce.printStackTrace();
}
}

// Save new Inventory Manger information


String inventorymanger=employeeID.getSelectedItem();

if(inventorymanger=="Inventory Manager" && e.getSource()==savenewemployeeButton)
{
try
{

String fullname1=nameTextField.getText();
String address1=usernameTextField.getText();
String username1=addressTextField.getText();
String password1=passwordTextField.getText();

String sql="insert into InventoryManagers values('"+fullname1+"','"+address1+"','"+username1+"','"+password1+"')";
st.executeUpdate(sql);

JOptionPane.showMessageDialog(null,"New Inventory Manager's details saved!");

}

catch(Exception ce)
{
ce.printStackTrace();
}
}

// Save new Administrator information


String administrator=employeeID.getSelectedItem();

if(administrator=="Administrator" && e.getSource()==savenewemployeeButton)
{
try
{
String fullname1=nameTextField.getText();
String address1=usernameTextField.getText();
String username1=addressTextField.getText();
String password1=passwordTextField.getText();

String sql="insert into Administrators values('"+fullname1+"','"+address1+"','"+username1+"','"+password1+"')";
st.executeUpdate(sql);

JOptionPane.showMessageDialog(null,"New Admin's details saved!");

}

catch(Exception ce)
{
ce.printStackTrace();
}
// ---------------------------------------- (End) Save new employee information ------------------------------------------------------------//
}

if(salesmen=="Sales Person" && e.getSource()==nextButton)
{
try
{
String sql="select * from SalesMen";
rs=st.executeQuery(sql);

// Problem code begins

if(rs.next())
{

nameTextField.setText(rs.getString("FullName"));
usernameTextField.setText(rs.getString("UserName"));
addressTextField.setText(rs.getString("Address"));

}

else

{
rs.previous();
System.out.println("boo!");
JOptionPane.showMessageDialog(null,"No more records");
}

}

// Problem code ends

catch(Exception ce)
{
ce.printStackTrace();
}


}

}

public static void main(String[] args)
{
new ManageEmployees();
}

}

最佳答案

  • 使用 Swing JComponents 而不是 AWT 组件

  • 使用 JTable 而不是 AWT 文本字段集

  • 搜索 ResultSetTableModel 或 TableFromDatabase 以获得简单、稳定和正确的解决方法

  • ResultSetTableModel、TableFromDatabase 或您的想法与事件调度线程有问题

关于java - 使用 AWT 按钮浏览并显示 'textfield' 中的每条数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17321913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com