请问为什么连接了数据库之后,只能显示出数据表的第一个数据呢,明明我的数据表里面存放了多条数据的!
我使用的数据源种类是Driver do Microsoft Access(*.mdb),
也试过使用Mircrosoft Access Driver(*.mdb),
代码如下,请各位高手指教:
package zhiwei;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class 商品管理 {
Statement sql;
Connection con;
ResultSet rs;
private JFrame frame;
final JLabel lblNull = new JLabel("\u6CA1\u6709\u4EFB\u4F55\u4FE1\u606F");
final List list = new List();
final JLabel label_3 = new JLabel("\u6CA1\u6709\u4EFB\u4F55\u4FE1\u606F");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){
}
try {
商品管理 window = new 商品管理();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public 商品管理() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.setBackground(Color.WHITE);
frame.setBounds(100, 100, 450, 334);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u5546\u54C1\u7BA1\u7406");
label.setFont(new Font("华文琥珀", Font.PLAIN, 36));
label.setForeground(Color.RED);
label.setBounds(243, 10, 150, 37);
frame.getContentPane().add(label);
lblNull.setBounds(327, 165, 89, 15);
frame.getContentPane().add(lblNull);
label_3.setBounds(327, 190, 89, 15);
frame.getContentPane().add(label_3);
list.setBackground(Color.CYAN);
list.setBounds(0, 0, 218, 300);
frame.getContentPane().add(list);
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 库存表");
while(rs.next()){
String name=rs.getString(2);
list.add(name);
con.close();
}
}catch(SQLException e1){
}
list.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 库存表");
int get=list.getSelectedIndex();
int duiying=0;
while(rs.next()){
if(duiying==get){
String bianhao=rs.getString(1);
String name=rs.getString(2);
String shuliang=rs.getString(4);
lblNull.setText(name);
label_3.setText(shuliang);
}
duiying++;
}
}catch(SQLException e1){
}
}
});
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(253, 57, 130, 98);
frame.getContentPane().add(lblNewLabel);
JLabel label_1 = new JLabel("\u7269\u54C1\u540D\u79F0");
label_1.setBounds(243, 165, 54, 15);
frame.getContentPane().add(label_1);
JLabel label_2 = new JLabel("\u7269\u54C1\u6570\u91CF");
label_2.setBounds(243, 190, 54, 15);
frame.getContentPane().add(label_2);
JButton button = new JButton("\u8FDB\u5165\u7269\u54C1\u7BA1\u7406");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button.setFont(new Font("华文琥珀", Font.PLAIN, 14));
button.setForeground(Color.RED);
button.setBounds(263, 215, 130, 25);
frame.getContentPane().add(button);
JButton button_1 = new JButton("\u9000\u51FA\u5E76\u56DE\u5230\u767B\u9646\u754C\u9762");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("delete * from 在线表 ");
}catch(SQLException e1){
}
new 总系统().main(null);
frame.dispose();
}
});
button_1.setForeground(Color.RED);
button_1.setBounds(232, 243, 184, 23);
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("\u8FD4\u56DE\u83DC\u5355");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new 菜单().main(null);
}
});
button_2.setBounds(277, 276, 93, 23);
frame.getContentPane().add(button_2);
}
}
[解决办法]
String name = "xxxx";
String sql = "update person_table set name = '" + name + "' where id = 1";
...