我收到了数据库错误,我无法弄清楚原因。
错误消息是:"当前语句的至少一个参数未初始化"
我认为这与这一行有关:
PreparedStatement stmt = cn.prepareStatement("INSERT INTO Ärende (ärendeid,kategori,budgeteradtid,status,kommentar)"
+ "VALUES (?,?,?,?,?)");
stmt.setString(1, getÄrendeID());
stmt.setString(2, getKategori());
stmt.setString(3, getbudgeteradtid());
stmt.setString(4, getStatus());
stmt.setString(5, getKommentar());
任何提示?
JFrame代码:
import java.awt.HeadlessException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
/**
*
* @author lrofke
*/
public class JFrameFormTest extends javax.swing.JFrame {
DefaultListModel listModel = new DefaultListModel();
/**
* Creates new form JFrameFormTest
*/
public JFrameFormTest() {
initComponents();
initÄrendeList();
}
private void initÄrendeList(){
lstÄrende.setModel(listModel);
//Töm lista
listModel.clear();
//Ladda listan med poster från db
Ärende s = new Ärende();
//Hämta lista med studenter
ArrayList <Ärende> ärendeList;
try {
ärendeList = s.getÄrende();
//Ladda lista med studenter
for (Ärende st : ärendeList){
listModel.addElement(st.toString());
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "FEL! " + ex.getMessage());
}
//Justera om "tabort"-knappen ska vara tillgänglig
int index = listModel.getSize();
if (index > 0){ //enable delete .
btnDelete.setEnabled(true);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtKategori = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtBudgeteradTid = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtStatus = new javax.swing.JTextField();
btnAdd = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
txtKommentar = new javax.swing.JTextField();
txtÄrendeID = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
lstÄrende = new javax.swing.JList();
btnDelete = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Student"));
jLabel1.setText("Kategori:");
txtKategori.setText("Kategori");
txtKategori.setName(""); // NOI18N
txtKategori.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtKategoriActionPerformed(evt);
}
});
jLabel2.setText("Budgeterad tid:");
txtBudgeteradTid.setText("Budgeterad tid");
txtBudgeteradTid.setToolTipText("");
txtBudgeteradTid.setName(""); // NOI18N
txtBudgeteradTid.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtBudgeteradTidActionPerformed(evt);
}
});
jLabel3.setText("Status");
txtStatus.setHorizontalAlignment(javax.swing.JTextField.LEFT);
txtStatus.setText("Status");
txtStatus.setName(""); // NOI18N
txtStatus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtStatusActionPerformed(evt);
}
});
btnAdd.setText("Lägg till ärende");
btnAdd.setToolTipText("");
btnAdd.setName(""); // NOI18N
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
jLabel4.setText("Kommentar");
txtKommentar.setText("Ange kommentar");
txtKommentar.setToolTipText("");
txtKommentar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtKommentarActionPerformed(evt);
}
});
txtÄrendeID.setText("ÄrendeID");
txtÄrendeID.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtÄrendeIDActionPerformed(evt);
}
});
jLabel5.setText("ÄrendeID");
jLabel5.setToolTipText("");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel3))
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtKategori, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
.addComponent(txtStatus))
.addGap(4, 4, 4)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtÄrendeID, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(39, 39, 39)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAdd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtBudgeteradTid, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtKommentar)))
.addGap(519, 519, 519))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAdd, txtBudgeteradTid});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtKategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(txtBudgeteradTid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtÄrendeID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(txtKommentar, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAdd))))
);
txtKategori.getAccessibleContext().setAccessibleName("");
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Objektorienterad programutveckling"));
lstÄrende.setModel(new javax.swing.AbstractListModel() {
String[] strings = { " " };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
lstÄrende.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
lstÄrende.setName("lstÄrende"); // NOI18N
jScrollPane1.setViewportView(lstÄrende);
btnDelete.setText("Ta bort");
btnDelete.setEnabled(false);
btnDelete.setName("btnRemove"); // NOI18N
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
jButton1.setText("Starta webapp");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(btnDelete))
.addContainerGap(40, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(btnDelete)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 3, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6))
);
jPanel2.getAccessibleContext().setAccessibleName("Ärendelista");
pack();
}// </editor-fold>
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Skapa nytt student-objekt och
//läs in värdena från resp. textbox i JFrame-dialogen
Ärende s = new Ärende();
s.setKategori(txtKategori.getText());
s.setbudgeteradtid(txtBudgeteradTid.getText());
s.setstatus(txtStatus.getText());
s.setÄrendeID(txtÄrendeID.getText());
s.setKommentar(txtKommentar.getText());
try {
//Anropa metod för att lägga till student
if (s.addÄrende().equals("success")){
//Ladda listan med studenter från db
initÄrendeList();
}else{
JOptionPane.showMessageDialog(this, "Kunde inte lägga till student!");
}
} catch (SQLException | HeadlessException ex) {
JOptionPane.showMessageDialog(this, "FEL! " + ex.getMessage());
}
}
private void txtKategoriActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(this, "Funktion ej implementerad!", "Meddelande", JOptionPane.WARNING_MESSAGE);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Öppna webbläsaren med den webbapplikation ni utvecklat
String url = "http://localhost:8080/D0007NJSFWebAppEx/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "FEL! " + ex.getMessage());
}
}
private void txtKommentarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtStatusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtBudgeteradTidActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtÄrendeIDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrameFormTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrameFormTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrameFormTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrameFormTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrameFormTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDelete;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList lstÄrende;
private javax.swing.JTextField txtBudgeteradTid;
private javax.swing.JTextField txtKategori;
private javax.swing.JTextField txtKommentar;
private javax.swing.JTextField txtStatus;
private javax.swing.JTextField txtÄrendeID;
// End of variables declaration
}
班级代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
*
* @author dsfsf
*/
public class Ärende {
private String kategori;
private String budgeteradtid;
private String status;
private String kommentar;
private String ärendeid;
public String getKategori() {
return kategori;
}
public void setKategori(String kategori) {
this.kategori = kategori;
}
public String getbudgeteradtid() {
return budgeteradtid;
}
public void setbudgeteradtid(String budgeteradtid) {
this.budgeteradtid = budgeteradtid;
}
public String getStatus() {
return status;
}
public void setstatus(String status) {
this.status = status;
}
public String getKommentar() {
return kommentar;
}
public void setKommentar(String kommentar) {
this.kommentar = kommentar;
}
public String getÄrendeID() {
return kommentar;
}
public void setÄrendeID(String ärendeid) {
this.ärendeid = ärendeid;
}
@Override
public String toString(){
return this.ärendeid + " Ärende ID " + this.kategori + " Kategori: " + this.budgeteradtid + " Budgeterad tid: " + this.status + " Status: " +
" Kommentar: " + this.kommentar;
}
public String addÄrende() throws SQLException {
//konrollera status på SQL-exekveringen
String sRet = "failure";
Connection cn = null;
try{
//Laddar db-library (derby) till applikationens minne
Class.forName("org.apache.derby.jdbc.ClientDriver");
//specificerar uppgifter som behövs för att koppla upp sig mot databasen
cn = DriverManager.getConnection("jdbc:derby://localhost:1527/Fiktivt_AB;create=true;user=fiktivt;password=fiktivt");
//Kontrollera uppkoppling mot db
if (cn == null){
throw new SQLException("Uppkoppling mot databas saknas!");
}
//specificerar vilket SQL-uttryck som ska köras mot db
PreparedStatement stmt = cn.prepareStatement("INSERT INTO Ärende (ärendeid,kategori,budgeteradtid,status,kommentar)"
+ "VALUES (?,?,?,?,?)");
stmt.setString(1, getÄrendeID());
stmt.setString(2, getKategori());
stmt.setString(3, getbudgeteradtid());
stmt.setString(4, getStatus());
stmt.setString(5, getKommentar());
//Kör SQL-uttryck
int i = stmt.executeUpdate();
//Kontrollera resultat av SQL-uttryck
if (i > 0)
sRet = "success";
return sRet; //returnera status från SQL-exekvering
}
catch (ClassNotFoundException | SQLException ex) {
throw new SQLException("Problem med db:" + ex.getMessage());
}finally{
if (cn!=null)
cn.close();
}
}
public ArrayList<Ärende> getÄrende() throws SQLException{
//Läs in information om db-uppkoppling
Connection cn = null;
//håller en lista med ett student-objekt för varje post i db
ArrayList ärendeList = new ArrayList<>();
try{
//Laddar db-library (derby) till applikationens minne
Class.forName("org.apache.derby.jdbc.ClientDriver");
//specificerar uppgifter som behövs för att koppla upp sig mot databasen
cn = DriverManager.getConnection("jdbc:derby://localhost:1527/Fiktivt_AB;create=true;user=fiktivt;password=fiktivt");
//Kontrollera uppkoppling mot db
if (cn == null){
throw new SQLException("Uppkoppling mot databas saknas!");
}
PreparedStatement stmt = cn.prepareStatement("SELECT kategori,budgeteradtid,status, kommentar FROM ärende "
+ "WHERE ärendeid = ?");
//Kör SQL-uttryck och ladda upp posterna i ResultSet variabel rs
ResultSet rs = stmt.executeQuery();
//Ladda lista med data från ResultSet (rs)
while (rs.next()){
//För varje post i db skapas ett student-objekt
Ärende s = new Ärende();
s.setKategori(rs.getString("kategori"));
s.setbudgeteradtid(rs.getString("budgeteradtid"));
s.setstatus(rs.getString("status"));
s.setKommentar(rs.getString("kommentar"));
//Lägg till student-objekt i arraylist
ärendeList.add(s);
}
return ärendeList;
}catch (ClassNotFoundException | SQLException ex) {
throw new SQLException("Problem med db:" + ex.getMessage());
}finally{
if (cn!=null)
cn.close();
}
}
}
答案 0 :(得分:0)
确保get
方法都没有返回null。您需要使用setNull
显式设置空值。
此外,我会避免使用öäå
字符,但评论和Strings
除外,因为在编码更改时可能会破坏。
答案 1 :(得分:0)
在此代码中:
PreparedStatement stmt = cn.prepareStatement("SELECT kategori,budgeteradtid,status, kommentar FROM ärende "
+ "WHERE ärendeid = ?");
//Kör SQL-uttryck och ladda upp posterna i ResultSet variabel rs
ResultSet rs = stmt.executeQuery();
在执行查询之前,您似乎没有为WHERE子句设置参数的值。