0
私はJAppletで非常に新しく、これを今やったことがあります。Jappletフォームデータベースから値を取得する
package myform;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JComboBox;
public class Form extends javax.swing.JApplet {
/** Initializes the applet Form */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
JComboBox list = new JComboBox();
try
{
String connstr="jdbc:oracle:thin:@localhost:1521/XE";
String uname="epolicia";
String pass="admin";
Class.forName("oracle.jdbc.OracleDriver").newInstance();
Connection connect =DriverManager.getConnection(connstr,uname,pass);
System.out.println("Oracle driver is loaded");
Statement state=connect.createStatement();
ResultSet rs = state.executeQuery("SELECT * FROM ZONE");
while (rs.next()) {
list.addItem(rs.getString(1));
//System.out.println(rs.getString(1));
}
}
catch(Exception ex)
{
list.addItem(ex);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
zoneList = new javax.swing.JComboBox();
addDistrict = new javax.swing.JButton();
cancelDistrict = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel1.setText("District ID :");
jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel2.setText("District Name :");
jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel3.setText("Zone :");
addDistrict.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
addDistrict.setText("Add");
addDistrict.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addDistrictActionPerformed(evt);
}
});
cancelDistrict.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
cancelDistrict.setText("Cancel");
cancelDistrict.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelDistrictActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
jLabel4.setText("Add New District");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(addDistrict)
.addGap(28, 28, 28)
.addComponent(cancelDistrict))
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
.addComponent(jTextField2)
.addComponent(zoneList, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(87, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel4)
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zoneList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(addDistrict)
.addComponent(cancelDistrict))
.addContainerGap(112, Short.MAX_VALUE))
);
}// </editor-fold>
private void addDistrictActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void cancelDistrictActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
// Variables declaration - do not modify
private javax.swing.JButton addDistrict;
private javax.swing.JButton cancelDistrict;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JComboBox zoneList;
// End of variables declaration
}
そして、私はこの出力に
を取得しています。しかし、値がコンボボックスに表示されていません。値段が必要です<option value='<%=rs.getString(1)%>'><%=rs.getString(2)%></option>
私を助けてください!事前に感謝
私はそれを得ました。しかし、このの解決策を教えてください。私は、コンボボックスのテキストが違っていて、価値が違う、このようなものが必要なことを意味します。 –
できますか? –