-2
我想將在JTextField中接受的roll no轉換爲整數值,以便它可以在MySQL中使用,但每當我點擊提交按鈕時,出現錯誤彈出說從字符串轉換爲整數的值不能在JDBC中輸入
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at random.student$1.actionPerformed(student.java:141)
這是我用
package random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class student extends JFrame{
int N = 0;
int i,j; // instance variables
String namex[] = new String[10]; // used in jdbc
String course[] = new String[10];
int roll_no[] = new int[10];
// jdbc
Connection conn = null;
PreparedStatement stmt = null;
public student() {
super("Student");
setLayout(null); // when layout is null, setBounds method is used with every window component to set the location and dimensions of every object
String no = JOptionPane.showInputDialog(null,"Enter the number of students"); // data is accepted in form of String
N = Integer.parseInt(no); // changes string to integer
JLabel lbl[] = new JLabel[N];
JLabel top[] = new JLabel[4];
JTextField name[] = new JTextField[N]; // Name
JTextField crse[] = new JTextField[N]; // Course
JTextField roll[] = new JTextField[N]; // roll no
// title bar
top[0] = new JLabel("SNo");
top[0].setBounds(200,8,50,30);
add(top[0]);
top[1] = new JLabel("Name");
top[1].setBounds(250,8,50,30);
add(top[1]);
top[2] = new JLabel("Roll No");
top[2].setBounds(380,8,50,20);
add(top[2]);
top[3] = new JLabel("Course");
top[3].setBounds(450,8,50,20);
add(top[3]);
// SNo
for(i = 0 ; i < N ; i++) {
lbl[i] = new JLabel((i+1)+"");
lbl[i].setBounds(200, j+30, 50, 30);
add(lbl[i]);
j = j + 30; // height component
}
// Name fields
j = 0;
for(i = 0 ; i < N ; i++) {
name[i] = new JTextField(30);
name[i].setBounds(250, j+30, 100, 20);
add(name[i]);
j = j + 30;
}
// roll no
j = 0;
for(i = 0 ; i < N ; i++) {
roll[i] = new JTextField(10);
roll[i].setBounds(380,j+30,50,20);
add(roll[i]);
j = j + 30;
}
// course
j = 0 ;
for(i = 0 ; i < N ; i++) {
crse[i] = new JTextField(20);
crse[i].setBounds(450, j + 30, 50, 20);
add(crse[i]);
j+= 30;
}
JButton btn = new JButton("Commit");
btn.setBounds(400,300,100,40);
add(btn);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent exp) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root",".dpadpep");
String sql = "Insert into student(roll_no,name,course) values(?,?,?)";
// Convert JTextField into String and integers
stmt = conn.prepareStatement(sql);
for(i = 0 ; i < N ; i++) {
namex[i] = name[i].getText(); // convert TextField to String
course[i] = crse[i].getText();
String text = roll[i].getText();
int rollno = Integer.parseInt(text); // first convert TextFIeld yo string and then into integer
stmt.setInt(1, rollno);
stmt.setString(2, namex[i]);
stmt.setString(3, course[i]);
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
});
setLocation(100, 100);
setVisible(true);
setSize(600,450);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new student();
}
}
我在不同的東西編輯此代碼的代碼。表已更新,但每次點擊提交時都會彈出錯誤。 請幫助。
我無法將文本字段中的字符串值轉換爲整數值。當TextField不是數組時,我已經完成了它,但它們是單獨工作的。
我建議你看看[如何使用表格](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html),以更好地管理用戶輸入。錯誤信息也告訴你你需要知道的一切,你需要調試代碼並找出它發生的原因並設計一個解決它的策略 – MadProgrammer