gpt4 book ai didi

java - 无法通过 Java 应用程序将数据添加到数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:07 25 4
gpt4 key购买 nike

我有一个疯狂的问题 :D 我有一个带有 Jtable 和显示、添加、删除和更新按钮的 Java 应用程序。与我的数据库的连接正常。

现在,当我将数据插入到我的 Textfields 并单击提交(添加)时,我可以在我的 Jtable 中看到该条目,但在我的数据库中看不到它。

这太疯狂了,因为我的数据库中已经有一些简单条目的数据,而且我的 JTable 正在显示数据,但是当我通过 java 应用程序插入数据时,我看不到我的条目 phpmyadmin 数据库 我连接到..

这很难解释,但也许你能找到问题所在:/

来自 JAVA JTABLE 的 TableView http://i.imgur.com/BrRUyx9.jpg

来自 PHPMYADMIN.COM 的 TableView http://i.imgur.com/TIbVzmc.jpg

我的代码

Controller 类

public void controllActionListenerAddEquipmentSubmitBtn(){
main.setActionListenerAddEquipmentSubmitBtn(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==main.getButtonAddEquipmentBtn()){
EquipmentDAO edao = new EquipmentDAO();
try {
edao.create(main.addEquipmentSubmit());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
}

大型机类...(主要)

 public Equipment addEquipmentSubmit(){
equipment = new Equipment();
equipment.setName(addEquipment.getName());
equipment.setSection(addEquipment.getSection());
equipment.setType(addEquipment.getType());
equipment.setValue(Double.parseDouble((addEquipment.getValue())));
equipment.setAmount(Integer.parseInt(addEquipment.getAmount()));
return equipment;
}

DAOCLASS

package DAOCollection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.mysql.jdbc.PreparedStatement;

import notesDBMySQL.DatabaseHandler;
import EntityClasses.Equipment;

public class EquipmentDAO implements DaoInterface{

private Connection con;
private java.sql.PreparedStatement sta;
private Statement std;
private ResultSet res;
private DatabaseHandler handle;
private String query;

@Override
public ResultSet show() throws SQLException{
handle = new DatabaseHandler();
Connection con = handle.buildConnectionToServer();
String query = "select EID as ID, EquipmentName as Name, EquipmentSection as Section, EquipmentType as Type, EquipmentValue as Value, EquipmentAmount as Amount from Equipment";
sta = con.prepareStatement(query);
res = sta.executeQuery(query);
return res;
}

@Override
public void create(Object c) throws SQLException {
handle = new DatabaseHandler();
con = handle.buildConnectionToServer();
std = con.createStatement();
Equipment equ = (Equipment) c ;
query = "INSERT INTO Equipment(EquipmentName,EquipmentSection,EquipmentType,EquipmentValue, EquipmentAmount)"+"VALUES('"+equ.getName()+"','"+equ.getSection()+"','"+equ.getType()+"','"+equ.getValue()+"','"+equ.getAmount()+"')";
std.execute(query);
con.commit();
handle.closeConnection();
}

@Override
public void update(Object c) throws SQLException {
Equipment equ = (Equipment) c;
handle = new DatabaseHandler();
con = handle.buildConnectionToServer();
query = "UPDATE Equipment SET name=?,section=?,type=?,value=?,amount=?,"+"WHERE EID=?";
sta = con.prepareStatement(query);
sta.setString(1, equ.getName());
sta.setString(2,equ.getSection());
sta.setString(3, equ.getType());
sta.setDouble(4, equ.getValue());
sta.setInt(5, equ.getAmount());
sta.setInt(6, equ.getEid());
sta.executeUpdate();
sta.close();
con.close();
handle.closeConnection();
}

@Override
public void delete(int id) throws SQLException {
handle = new DatabaseHandler();
con = handle.buildConnectionToServer();
query = "DELETE FROM EQUIPMENT"+"WHERE EID="+id;
sta.executeUpdate(query);
sta.close();
con.close();
handle.closeConnection();
}
}

实体类

package EntityClasses;

public class Equipment {

private int eid;
private String name;
private String section;
private String type;
private double value;
private int amount;

public int getEid() {
return eid;
}

public void setEid(int eid) {
this.eid = eid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSection() {
return section;
}

public void setSection(String section) {
this.section = section;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public double getValue() {
return value;
}

public void setValue(double value) {
this.value = value;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}


}

添加类(class)

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddEquipment extends JPanel {
private JTextField txtu2;
private JTextField txtu3;
private JTextField txtu4;
private JTextField txtu5;
private JTextField txtu6;

private JButton btnu8;
private JButton btnu9; //Back Button

public AddEquipment() {
setLayout(null);

JLabel lblu8 = new JLabel("Add Equipment");
lblu8.setForeground(Color.WHITE);
lblu8.setFont(new Font("Tahoma", Font.BOLD, 16));
lblu8.setBounds(12, 13, 319, 33);
add(lblu8);

JLabel lblu3 = new JLabel("Name");
lblu3.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu3.setBounds(60, 91, 46, 14);
add(lblu3);

txtu2 = new JTextField();
txtu2.setColumns(10);
txtu2.setBounds(221, 88, 377, 22);
add(txtu2);

JLabel lblu4 = new JLabel("Section");
lblu4.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu4.setBounds(60, 156, 69, 14);
add(lblu4);

txtu3 = new JTextField();
txtu3.setColumns(10);
txtu3.setBounds(221, 153, 377, 22);
add(txtu3);

JLabel lblu5 = new JLabel("Type");
lblu5.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu5.setBounds(60, 221, 46, 14);
add(lblu5);

txtu4 = new JTextField();
txtu4.setColumns(10);
txtu4.setBounds(221, 218, 377, 22);
add(txtu4);

JLabel lblu6 = new JLabel("Value");
lblu6.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu6.setBounds(60, 286, 46, 14);
add(lblu6);

txtu5 = new JTextField();
txtu5.setColumns(10);
txtu5.setBounds(221, 283, 377, 22);
add(txtu5);

JLabel lblu7 = new JLabel("Amount");
lblu7.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu7.setBounds(60, 351, 69, 14);
add(lblu7);

txtu6 = new JTextField();
txtu6.setColumns(10);
txtu6.setBounds(221, 348, 377, 22);
add(txtu6);

btnu8 = new JButton("Submit");
btnu8.setFont(new Font("Tahoma", Font.PLAIN, 13));

//btnu8.setBackground(Color.BLACK); // button color
//btnu8.setBackground( new Color(0, 0, 0, 50) ); //opacity

btnu8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});

btnu8.setBounds(422, 534, 110, 30);
add(btnu8);

btnu9 = new JButton("Back");
btnu9.setFont(new Font("Tahoma", Font.PLAIN, 13));
btnu9.setBounds(542, 534, 110, 30);
add(btnu9);

JLabel lblback3 = new JLabel("");
lblback3.setIcon(new ImageIcon("\\\\I-INTRA-03\\IIS-Lehre\\DAPRO-WF4.informatik.hs-ulm.de\\15\\group_together\\GUI_graphics\\background_panel.jpg"));
lblback3.setBounds(0, 0, 664, 577);
add(lblback3);
}

//Getter-Method for the Back Button "btnu9"
public JButton getBackAddEquipment(){
return btnu9;
}

//Getter-Method for the "Submit" Button "btnu8"
public JButton getAddEquipmentBtn(){
return btnu8;
}

public String getName() {
return this.txtu2.getText();
}

// method sets the string value of the textfield
public void setName(String txtu2) {
this.txtu2.setText(txtu2);
}

public String getSection() {
return this.txtu3.getText();
}

public void setSection(String txtu3) {
this.txtu3.setText(txtu3);
}

public String getType() {
return this.txtu4.getText();
}

public void setType(String txtu4) {
this.txtu4.setText(txtu4);
}

public String getValue() {
return this.txtu5.getText();

}

public void setValue(String txtu5) {
this.txtu5.setText(txtu5);
}

public String getAmount() {
return this.txtu6.getText();
}

public void setAmount(String txtu6) {
this.txtu6.setText(txtu6);
}
}

最佳答案

您的 DAO 实现是在自找麻烦。您永远不应该返回 ResultSet。更好的想法是将数据加载到对象或数据结构中,并在 finally block 的方法范围内关闭 ResultSet。您所有的数据库资源都一样。

关于java - 无法通过 Java 应用程序将数据添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31123133/

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