怎么将下拉列表中的数据添加到数据库中,例如我的这个代码,下拉列表是管理员和普通员工,其他的都能添加,但是下拉列表项无数据
package Denglu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Zhuce extends JFrame implements ActionListener{
private static final Statement PraparedStatement = null;
Connection con;
String [] str = {"管理员","普通员工"};
JLabel xm = new JLabel("姓名");
JLabel ygtel = new JLabel("手机号");
JLabel ps = new JLabel("密码");
JLabel cplb = new JLabel("权限");
JComboBox cplb1 = new JComboBox(str);
JLabel banner = new JLabel();
ImageIcon beijing =new ImageIcon("image/1.jpg");
JTextField xm1 = new JTextField(10);
JTextField tel1 = new JTextField(10);
JTextField ps1 = new JTextField(10);
JButton b1 = new JButton("注册");
public void del() {
setTitle("添加商品信息");
setLayout(null);
xm.setBounds(180,90,100,50);
xm1.setBounds(250,103,200,20);
ygtel.setBounds(180,120,100,50);
tel1.setBounds(250,133,200,20);
ps.setBounds(180,150,100,50);
ps1.setBounds(250,163,200,20);
cplb.setBounds(180,180,100,50);
cplb1.setBounds(250,193,200,20);
banner.setIcon(beijing);
banner.setBounds(0,-25,1200,400);
b1.setBounds(180,300, 80, 30);
add(xm);
add(xm1);
add(ygtel);
add(tel1);
add(ps);
add(ps1);
add(cplb);
add(cplb1);
add(banner);
add(b1);
b1.addActionListener(this);
setVisible(true);
setBounds(400,300,960,400);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) { //如果你点击的是确认按钮
String s1=xm1.getText(); //获取文本框中的信息
String s2=tel1.getText();
String s3=ps1.getText();
String s4=cplb1.getEditor().getItem().toString();
//加载数据库驱动
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e1) {
}
//连接数据库
String uri ="jdbc:mysql://localhost:3306/gsglxt?characterEncoding=gbk";
String user = "root"; //数据库用户名
String password = "123456"; //数据库密码
try {
con = DriverManager.getConnection(uri,user,password);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JOptionPane.showMessageDialog(null, "数据库连接成功", "连接数据库",
JOptionPane.INFORMATION_MESSAGE);
try {
if(init()==false){ //如果没有,则执行添加
String sql="insert into yguser(ygadmin,ygtel,ygpassword,ygqx)"+"values('"+s1+"','"+s2+"','"+s3+"','"+s4+"')"; //创建sql语句
Statement statement = null;
try {
statement = con.createStatement();
} catch (SQLException e2) {
e2.printStackTrace();
}
try {
int result = statement.executeUpdate(sql); //执行sql语句
} catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "注册成功", "完成",
JOptionPane.INFORMATION_MESSAGE);
dispose();
}
} catch (HeadlessException | SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
//判断添加的Id在数据库中是否存在的方法
public boolean init() throws SQLException {
String s1 = null;
String sql1="select ygtel FROM yguser where ygtel='"+s1+"'";
PreparedStatement pstm = con.prepareStatement(sql1);// 执行查询
ResultSet rs = pstm.executeQuery();
boolean result = false;
while(rs.next()) {
result = true;
}
rs = pstm.executeQuery();
return result;
}
public static void main(String args[]){
new Zhuce();
}
}