我正在嘗試製作一個應用程序,該應用程序將用戶添加到我的SQL服務器的一些字符串。然而,當我試圖將一個字符串從JTextField中它被添加兩次添加到我的JList ..Defaultlistmodel從jtextfield中添加兩個項目
這裏是東西
用戶增加了JTextField的名稱。當他打的+按鈕,它也會發送到的jList
public void addBrand() {
int index = BrandList.getSelectedIndex(); // get selected index
if (index == -1) { // no selection, so insert at beginning
index = 0;
}
else { // add after the selected item
index++;
}
model.insertElementAt(BrandLbl.getText(), index);
BrandLbl.setText(null);
}
都在這裏很好,我看到一個產品加入我的JList
當用戶決定列表完成,他打「下一步」按鈕, 在sendArraytoDB(JList的列表)方法被調用
public static void sendArraytoDB(JList<String> list){
Connection con = null;
PreparedStatement stm = null;
String updQuery = "insert into brand_names (name) values (?)";
try{
con = DB.getConnection();
//con.setAutoCommit(false);
int x =1;
stm = con.prepareStatement(updQuery);
int f = list.getModel().getSize();
System.out.print(f);
for (int i=0; i<list.getModel().getSize(); i++){
String name =list.getModel().getElementAt(i);
stm.setString(x, name);
//try{
stm.executeUpdate();
//}finally{
//stm.close();
//}
}
}catch(SQLException ex){
System.out.printf("error while sending array to db");
ex.printStackTrace();
}finally{
if (stm != null){
等等等等....
我的運氣不好我DATABSE顯示,有兩個南ES發送.. 所以它像
aa brand
1
2 "the string i sent"
列表中有一個永諾多個空的記錄我的記錄之前,我不能發表圖片... 想看看跆拳道正在發生的事情我數了數列表的大小就在我把它
int f = list.getModel().getSize();
System.out.print(f);
,答案是2 ...如果我進入3記錄其6 ..等等
我縮小了問題的模型,因爲改變addBrand()方法來
public void addBrand() {
String all = "xghxc";
model.addElement(all);
}
厚顏無恥顯示了兩個「xghxc」被添加到我的名單在同一時間在我自己的眼睛驚奇
我搜索谷歌前面,但它甚至不必須有類似的問題,以礦山:(
我需要的是一個代碼或諮詢或不便以點我不加入之間我記錄一個空的無用記錄
這裏是我的全部代碼的人誰擁有耐心和時間
MyMain.java
package tweGraf;
import javax.swing.JFrame;
public class MyMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Gui g = new Gui();
DB.MakePool();
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.setSize(1000, 800);
g.setVisible(true);
}
}
Gui.java
package tweGraf;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Gui extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frameYesNo = new JFrame();
String message = "all data will perish. are you sure";
private JPanel Container = new JPanel(); // panels
private JPanel FirstPanel = new JPanel();
private JPanel NewSession = new JPanel();
private JPanel LoadSession = new JPanel();
private JPanel LoadList = new JPanel();
private JPanel GraphSub1 = new JPanel();
private JPanel GraphSub2 = new JPanel();
private JPanel GraphSub3 = new JPanel();
private JTabbedPane GraphPanel = new JTabbedPane();
private JButton NewSessBtn = new JButton(); // buttons
private JButton LoadSessBtn = new JButton();
private JButton BackFP = new JButton();
private JButton plusBrand = new JButton();
private JButton minusBrand = new JButton();
private JButton Next = new JButton();
private JLabel EnterBrandLbl = new JLabel(
"Please insert brands for analysis "); // Labels
private JTextField BrandLbl = new JTextField(20); // textfields
public DefaultListModel<String> model = new DefaultListModel<String>
public JList BrandList = new JList(model); // list
private JScrollPane MyScrollPane = new JScrollPane(BrandList);
private CardLayout cardLayout = new CardLayout(); // layouts
private GridBagLayout MyLayout = new GridBagLayout();
GridBagConstraints MyConstr = new GridBagConstraints();
public Gui() {
super("twegraph");
NewSessBtn.setText("New Session"); // button configuration
LoadSessBtn.setText("Load Session");
BackFP.setText("Back");
plusBrand.setText("+");
minusBrand.setText("-");
Next.setText("Next");
actionListener al = new actionListener();
NewSessBtn.addActionListener(al); // add action listeners
LoadSessBtn.addActionListener(al);
BackFP.addActionListener(al);
plusBrand.addActionListener(al);
minusBrand.addActionListener(al);
Next.addActionListener(al);
plusBrand.addActionListener(al);
minusBrand.addActionListener(al);
Container.setLayout(cardLayout); // panels to container+
Container.add(FirstPanel, "FirstPanel");
Container.add(NewSession, "NewSession");
Container.add(LoadSession, "LoadSession");
Container.add(GraphPanel, "GraphPanel");
Container.add(LoadList, "LoadList");
FirstPanel.setLayout(MyLayout); // first panel
MyConstr.gridwidth = 3;
MyConstr.gridheight = 3;
MyConstr.weightx = 1.0;
MyConstr.weighty = 1.0;
MyConstr.ipadx = 100;
MyConstr.ipady = 50;
MyConstr.insets = new Insets(50, 20, 50, 20);
MyConstr.gridx = 1;
MyConstr.gridy = 0;
MyConstr.anchor = GridBagConstraints.NORTH;
MyLayout.setConstraints(NewSessBtn, MyConstr);
FirstPanel.add(NewSessBtn);
MyConstr.gridx = 1;
MyConstr.gridy = 2;
MyConstr.anchor = GridBagConstraints.SOUTH;
MyLayout.setConstraints(LoadSessBtn, MyConstr);
FirstPanel.add(LoadSessBtn);
NewSession.setLayout(MyLayout); // New Session panel
MyConstr.gridwidth = 3;
MyConstr.gridheight = 3;
MyConstr.ipadx = 0; // size
MyConstr.ipady = 0; // size
MyConstr.gridx = 0;
MyConstr.gridy = 2;
MyConstr.insets = new Insets(10, 20, 10, 20);
MyConstr.anchor = GridBagConstraints.SOUTHWEST;
MyLayout.setConstraints(BackFP, MyConstr);
NewSession.add(BackFP);
MyConstr.anchor = GridBagConstraints.SOUTHEAST;
MyLayout.setConstraints(Next, MyConstr);
NewSession.add(Next);
MyConstr.ipadx = 0; // size
MyConstr.ipady = 0; // size
MyConstr.gridx = 0; // place
MyConstr.gridy = 1; // place
MyConstr.insets = new Insets(0, 0, 0, 0);
MyConstr.anchor = GridBagConstraints.PAGE_START;
MyLayout.setConstraints(EnterBrandLbl, MyConstr);
NewSession.add(EnterBrandLbl);
MyConstr.gridx = 0;
MyConstr.gridy = 1;
MyConstr.anchor = GridBagConstraints.CENTER;
MyLayout.setConstraints(BrandLbl, MyConstr);
NewSession.add(BrandLbl);
MyConstr.gridx = 2;
MyConstr.gridy = 1;
MyConstr.anchor = GridBagConstraints.LAST_LINE_START;
MyLayout.setConstraints(plusBrand, MyConstr);
NewSession.add(plusBrand);
MyConstr.gridx = 2;
MyConstr.gridy = 1;
MyConstr.anchor = GridBagConstraints.LAST_LINE_END;
MyLayout.setConstraints(minusBrand, MyConstr);
NewSession.add(minusBrand);
MyConstr.ipadx = 0; // size
MyConstr.ipady = 0;
MyConstr.gridx = 0;
MyConstr.gridy = 1;
MyConstr.anchor = GridBagConstraints.SOUTH;
MyLayout.setConstraints(MyScrollPane, MyConstr);
NewSession.add(MyScrollPane);
GraphPanel.addTab("overall",GraphSub1); //Graph panel
GraphPanel.addTab("tweets/time",GraphSub2);
GraphPanel.addTab("fame",GraphSub3);
this.setContentPane(Container);
cardLayout.show(Container, "FirstPanel");
}
public class actionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JButton src = (JButton) event.getSource();
int answer = 0;
if (src.equals(NewSessBtn))
{
answer = JOptionPane.showConfirmDialog(frameYesNo, message);
if (answer == JOptionPane.YES_OPTION) {
cardLayout.show(Container, "NewSession");
try {
DB.flushData();
} catch (SQLException ex) {
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (answer == JOptionPane.NO_OPTION) {
frameYesNo.dispose();
}
}
if (src.equals(LoadSessBtn)){
cardLayout.show(Container, "LoadSession");
}
if (src.equals(BackFP)){
cardLayout.show(Container, "FirstPanel");
}
if (src.equals(Next)){
cardLayout.show(Container, "GraphPanel");
DB.sendArraytoDB(BrandList);
}
if (src.equals(plusBrand)){
addBrand();
}
if (src.equals(minusBrand))
{
removeBrand();
}
}
}
public void addBrand() {
/*int index = BrandList.getSelectedIndex(); // get selected index
if (index == -1) { // no selection, so insert at beginning
index = 0;
}
else { // add after the selected item
index++;
}*/
String all = "xghxc";
//model.insertElementAt(BrandLbl.getText(), index);
model.addElement(all);
//BrandLbl.setText(null);
}
public void removeBrand() {
int index2 = BrandList.getSelectedIndex();
if (index2 != -1){
model.remove(index2);
}
int size = model.getSize();
if (size == 0) {
minusBrand.setEnabled(false);
} else {
//if (index == model.getSize()) {
//index--;
//}
}
}
}
DB.java
package tweGraf;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
/**
*
* @author cheval
*/
public class DB {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/twegrahpdb";
static final String USER = "root";
static final String PASS = "Xrt38H0a";
private static ComboPooledDataSource cdps = new ComboPooledDataSource();
public static void MakePool(){
try {
cdps = new ComboPooledDataSource();
cdps.setDriverClass(JDBC_DRIVER);
cdps.setJdbcUrl(DB_URL);
cdps.setUser(USER);
cdps.setPassword(PASS);
cdps.setMaxPoolSize(50);
cdps.setMaxStatements(50);
}catch(Exception ex){
System.out.printf("error smth wrong happened");
}
}
public static Connection getConnection() throws SQLException{
return cdps.getConnection();
}
public static void flushData() throws SQLException{
Statement stm = null;
Connection con = null;
try{
con = DB.getConnection();
stm = con.createStatement();
String flushquery1 = "TRUNCATE json_cache";
String flushquery2 = "TRUNCATE tweets";
String flushquery3 = "TRUNCATE tweet_mentions";
String flushquery4 = "TRUNCATE tweet_tags";
String flushquery5 = "TRUNCATE tweet_urls";
String flushquery6 = "TRUNCATE users";
String flushquery7 = "TRUNCATE brand_names";
stm.executeUpdate(flushquery1);
stm.executeUpdate(flushquery2);
stm.executeUpdate(flushquery3);
stm.executeUpdate(flushquery4);
stm.executeUpdate(flushquery5);
stm.executeUpdate(flushquery6);
stm.executeUpdate(flushquery7);
}catch (SQLException e) {
System.out.printf("error executing db clear");
} finally {
if (stm != null){
try{
stm.close();
System.out.printf("statement closed successfuly \n");
} catch (SQLException e){
System.out.printf("error closing statement");
}
}
if (con != null){
try{
con.close();
System.out.printf("connection closed succesfully \n");
} catch (SQLException e){
System.out.printf("error closing connection");
}
}
}
}
public static void sendArraytoDB(JList<String> list){
Connection con = null;
PreparedStatement stm = null;
String updQuery = "insert into brand_names (name) values (?)";
try{
con = DB.getConnection();
//con.setAutoCommit(false);
int x =1;
stm = con.prepareStatement(updQuery);
int f = list.getModel().getSize();
System.out.print(f);
for (int i=0; i<list.getModel().getSize(); i++){
String name =list.getModel().getElementAt(i);
stm.setString(x, name);
//try{
stm.executeUpdate();
//}finally{
//stm.close();
//}
}
}catch(SQLException ex){
System.out.printf("error while sending array to db");
ex.printStackTrace();
}finally{
if (stm != null){
try {
stm.close();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (con != null){
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
,如果你仍然不知道不約我的實際問題,但護理STILL看到水木清華錯誤關於我的編碼風格或我的技術plz發佈它
感謝您的時間
'請參閱我的編碼風格錯誤 - 變量名稱不應以大寫字符開頭。注意論壇如何突出顯示他們就像一個類名。另外類名應該以大寫字符開頭。 – camickr
thnks的意見 –