日期:2014-05-20  浏览次数:20864 次

javase数据库问题
这是我做得一个简单的swing窗体,但在点击提交将数据传到数据库后,stuName和stuSex为什么会出现“??”呢,是不是字段类型类型的关系呢?
StudentFrame.java这是主类:
public class StudentFrame {

private StudentBean sb; //创建StudentBean对象
private DbUtil db; //创建数据库连接对象

public StudentFrame(){
JFrame jframe = new JFrame("Student Management System");
Container con = jframe.getContentPane();
jframe.setLayout(null);

//创建JLabel组件
JLabel jlabel1 = new JLabel("学号:");
JLabel jlabel2 = new JLabel("姓名:");
JLabel jlabel3 = new JLabel("性别:");
//设置组件的位置和大小
jlabel1.setBounds(20,20,150,100);
jlabel2.setBounds(20,100,150,100);
jlabel3.setBounds(20,180,150,100);
con.add(jlabel1);
con.add(jlabel2);
con.add(jlabel3);

//创建JTextField组件
final JTextField text1 = new JTextField(10);
final JTextField text2 = new JTextField(10);
final JTextField text3 = new JTextField(10);
//设置组件的位置和大小
text1.setBounds(100,50,150,30);
text2.setBounds(100,130,150,30);
text3.setBounds(100,210,150,30);
con.add(text1);
con.add(text2);
con.add(text3);

//创建JButton组件
JButton button1 = new JButton("提交");
JButton button2 = new JButton("退出");
JButton button3 = new JButton("显示学生信息");
//设置组件的位置和大小
button1.setBounds(20,270,80,30);
button2.setBounds(110,270,80,30);
button3.setBounds(200,270,120,30);
con.add(button1);
con.add(button2);
con.add(button3);

//为按钮组件添加事件侦听器

//为buuton1添加事件侦听器
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sb = new StudentBean();

int stuId = Integer.parseInt(text1.getText());//将text1的字段转换为int类型
String stuName = String.valueOf(text2.getText());
String stuSex = String.valueOf(text3.getText());

sb.setStuId(stuId);
sb.setStuName(stuName);
sb.setStuSex(stuSex);

if(sb.getStuId() != 0 && !sb.getStuName().equals("") && !sb.getStuSex().equals("")){
try{
db = new DbUtil();
db.insert(sb);
}catch(Exception ex){
ex.printStackTrace();
}
finally{
text1.setText("");
text2.setText("");
text3.setText("");
}
}
}
});

//为button2添加事件侦听器
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});

//设置Swing的大小并设置为可见的
jframe.setSize(400,400);
jframe.setVisible(true);
}

public static void main(String[] args){
new StudentFrame();
}
}

DbUtil.java数据库连接

public class DbUtil {
private Connection conn;
PreparedStatement pre,pre1;

public Connection getConnection(){
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "910323";
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,username,password);
return conn;
}catch(Exception ex){
ex.printStackTrace();
}
return conn;
}

public int insert(StudentBean sb) throws SQLException{
int temp = 0;
int stuId = sb.getStuId();
String stuName = sb.getStuName();
String stuSex = sb.getStuSex();
try{
conn = getConnection();
String sql = "insert into student2(stuId,stuName,stuSex)"+"value(?,?,?)";
pre = conn.prepareStatement(sql);
pre.setInt(1, stuId);
pre.setString(2,stuName);
pre.setString(3,stuSex);
temp = pre.executeUpdate();