gpt4 book ai didi

java - 用java编程数据库

转载 作者:行者123 更新时间:2023-12-01 14:27:16 25 4
gpt4 key购买 nike

我注意到,当我从 GUI 将数据发送到数据库时,我创建的数据保存在那里,但是当我尝试调用它时,什么也没有发生。我还注意到,创建的结果集仅检索 java 连接到数据库时保存的记录,例如如果在创建结果集时,假设某个数据库表中有 500 条记录,则只能检索这 500 条记录,而添加后无法检索其他记录。

我已经尝试了所有我迫切需要的帮助或为我指出正确的方向

这是负责检索记录的按钮的代码片段

JButton btncanel = new JButton("Submit");
btncanel.setBounds(820, 300, 80, 30);
btncanel.setFont(new Font("Times New Roman", Font.BOLD,12));
panel.add(btncanel);
btncanel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (scat3.getSelectedIndex() == 1) {

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String FILE = "jdbc:odbc:Usermbj";
Connection pcon = DriverManager.getConnection(FILE);
java.sql.Statement pt = pcon.createStatement();
ResultSet result = pt.executeQuery("SELECT * FROM Airlines2");

while (result.next()) {
String LN = result.getString(3);
String FN = result.getString(4);
String COMP = result.getString(12);
int RAPnum = result.getInt(1);
String EH = result.getString(2);
String FRP = result.getString(5);
String ROD = result.getString(6);
int RS = result.getInt(7);
String OD = result.getString(8);
int OS = result.getInt(9);
String COOD =result.getString(10);
String AS = result.getString(11);

if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&& (scat4.getSelectedIndex() == 1 && c.getText().equals(FN))) {

JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);

lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}

if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&& (c.getText().equals(""))) {

JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender-----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);

lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}

if ((scat4.getSelectedIndex() == 1 && c.getText().equals(FN))
&&(crit.getText().equals(""))) {

JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors-----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);

lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
}
} catch(Exception g) {
g.printStackTrace();
}
} else if (scat3.getSelectedIndex() == 2) {

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String FILE = "jdbc:odbc:Usermbj";
Connection pcon = DriverManager.getConnection(FILE);
java.sql.Statement qt = pcon.createStatement();
ResultSet result = qt.executeQuery("SELECT * FROM Concessionaire1");

while (result.next()) {
String LN = result.getString(3);
String FN = result.getString(4);
String COMP = result.getString(12);
int RAPnum = result.getInt(2);
String EH = result.getString(1);
String FRP = result.getString(5);
String ROD = result.getString(6);
int RS = result.getInt(7);
String OD = result.getString(8);
int OS = result.getInt(9);
String COOD =result.getString(10);
String AS = result.getString(11);

if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&& (scat4.getSelectedIndex() == 1 && c.getText().equals(FN))) {

JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);

lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}

if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&&(c.getText().equals(""))){

JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);

lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}

if ((scat4.getSelectedIndex() == 1 && c.getText().equals(FN))
&&(crit.getText().equals(""))) {

JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);

lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
}
} catch (Exception g) {
g.printStackTrace();
}
}
}
});

最佳答案

首先,如果你打开一个连接或者创建一个语句,你应该在使用后关闭它,否则你很快就会耗尽资源。

其次,根据数据库,您可能必须提交更改,否则事务可能会回滚并且数据会丢失。

关于java - 用java编程数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108791/

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