无法通过 Java 应用程序将数据添加到数据库
Posted
技术标签:
【中文标题】无法通过 Java 应用程序将数据添加到数据库【英文标题】:Can´t add data through Java application to database 【发布时间】:2015-09-16 08:33:06 【问题描述】:我有一个疯狂的问题:D 我有一个带有Jtable
和显示、添加、删除和更新按钮的 Java 应用程序。与我的数据库的连接有效。
现在,当我将数据插入我的Textfields
并单击提交(添加)时,我可以在我的Jtable
中看到该条目,但在我的数据库中看不到它。
这太疯狂了,因为我的数据库中已经有一些简单的条目,而我的 JTable
正在显示数据,但是当我通过 java 应用程序插入数据时,我看不到我的 phpmyadmin database
中的条目我已连接..
这很难解释,但也许你能找到问题所在:/
来自 JAVA JTABLE 的表格视图 http://i.imgur.com/BrRUyx9.jpg
来自 PHPMYADMIN.COM 的表格视图 http://i.imgur.com/TIbVzmc.jpg
我的代码
控制器类
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();
);
大型机类...(MAIN)
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;
添加类
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);
【问题讨论】:
【参考方案1】:您的 DAO 实现正在自找麻烦。你永远不应该返回ResultSet
。更好的想法是将数据加载到对象或数据结构中,并在 finally 块中关闭方法范围内的ResultSet
。所有数据库资源都一样。
【讨论】:
首先感谢您的评论...您知道我的一个朋友有相同的代码..真的..我的意思是我不明白问题是什么。是的,你对结果集是正确的,但他有相同的代码,他的程序正在将数据添加到数据库中:/ 我只是在我的 jtable 中而不是在我的真实数据库中(查看 phpmyadmin)@duffymo 那么你和你的朋友都错了。我想知道您是否拥有来自客户端 IP 地址的适当权限。请与您的 MySQL 确认。您的朋友可能已获得许可,但您没有。 谢谢我们都改变了它......我们现在使用数组列表而不是结果集,并通过循环保护对象中的数据,然后传递它:)以上是关于无法通过 Java 应用程序将数据添加到数据库的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中加载动态项目时无法将页脚视图添加到列表视图
无法将 extra_context 添加到 ListView