我有一个疯狂的问题:D我有一个带有Jtable
的Java应用程序以及show,add,delete和update按钮。与我的数据库的连接有效。
现在当我将数据插入我的Textfields
并点击提交(添加)时,我可以看到Jtable
中的条目,但我无法在我的数据库中看到它。
这很疯狂,因为我已经在我的数据库中有一些简单条目的数据,我的JTable
显示数据但是当我通过java应用程序插入数据时,我看不到我{{1}中的条目我所连接的......
很难解释,但也许你可以找到问题:/
从JAVA JTABLE查看表格 http://i.imgur.com/BrRUyx9.jpg
PHPMYADMIN.COM的表格视图 http://i.imgur.com/TIbVzmc.jpg
我的代码
CONTROLLER CLASS
phpmyadmin database
MAINFRAME CLASS ...(主要)
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();
}
}
}
});
}
DAOCLASS
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;
}
ENTITY CLASS
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;
}
}
答案 0 :(得分:1)
您的DAO实施正在寻找麻烦。你永远不应该归还ResultSet
。更好的想法是将数据加载到对象或数据结构中,并关闭finally块中方法范围中的ResultSet
。所有数据库资源都相同。