我有以下代码,编译得很好,但当它运行时,它会抛出异常,java.lang. lang。ararindexoutofboundsexception:11,请如果你能帮助什么是错误的,这将是一个很大的帮助。
代码是:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Pro implements ActionListener
{
JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13;
JButton b1,b2;
public Pro()
{
JFrame f = new JFrame();
JLabel l1 = new JLabel("SNo.");
JLabel l2 = new JLabel("OPANo");
JLabel l3 = new JLabel("CollegeName");
JLabel l4 = new JLabel("ProjectName");
JLabel l5 = new JLabel("SanctionNoDate");
JLabel l6 = new JLabel("TotalOutlayInLakhs)");
JLabel l7 = new JLabel("ProjectDuration");
JLabel l8 = new JLabel("AmountReleased");
JLabel l9 = new JLabel("BalanceToBeReleased");
JLabel l10 = new JLabel("PRSGsHeld");
JLabel l11 = new JLabel("NextPRSGDue");
JLabel l12 = new JLabel("CompletionMonth");
JLabel l13 = new JLabel("Status");
t1 = new JTextField(20);
t1.setEnabled(false);
t2 = new JTextField(20);
t3 = new JTextField(20);
t4 = new JTextField(20);
t5 = new JTextField(20);
t6 = new JTextField(20);
t7 = new JTextField(20);
t8 = new JTextField(20);
t9 = new JTextField(20);
t10 = new JTextField(20);
t11 = new JTextField(20);
t12 = new JTextField(20);
t13 = new JTextField(20);
b1 = new JButton("Reset");
b2 = new JButton("Insert");
b1.addActionListener(this);
b2.addActionListener(this);
JPanel p1 = new JPanel();
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(t2);
p1.add(l3);p1.add(t3);
p1.add(l4);p1.add(t4);
p1.add(l5);p1.add(t5);
p1.add(l6);p1.add(t6);
p1.add(l7);p1.add(t7);
p1.add(l8);p1.add(t8);
p1.add(l9);p1.add(t9);
p1.add(l10);p1.add(t10);
p1.add(l11);p1.add(t11);
p1.add(l12);p1.add(t12);
p1.add(l13);p1.add(t13);
p1.add(b1);p1.add(b2);
p1.setLayout(new GridLayout(14,2));
f.add(p1);
f.pack();
f.setResizable(false);
f.setVisible(true);
}
public int getMaxSNO()
{
int sno=0;
String path ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=doit.mdb";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(path);
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("SELECT max(Sno) from project");
if(rs.next())
{
sno=rs.getInt(1);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.toString());
}
return sno;
}
public boolean isAlpha(String str)
{
boolean result = false;
for(int i=0;i!=str.length();i++)
{
int ch = str.charAt(i);
if((ch>=65 && ch<=91) || (ch>=97 && ch<=122))
{
result = true;
}
else
{
result = false;
break;
}
}
return result;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
t7.setText("");
t8.setText("");
t9.setText("");
t10.setText("");
t11.setText("");
t12.setText("");
t13.setText("");
}
else if(e.getSource()==b2)
{
String name = t3.getText();
boolean b1 = isAlpha(name);
if(b1)
{
try
{
String path ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=doit.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(path);
PreparedStatement ps = con.prepareStatement("INSERT into project(OPANo, CollegeName, ProjectName, SanctionNoDate, TotalOutlayInLakhs, ProjectDuration, AmountReleased, BalanceToBeReleased, PRSGsHeld, NextPRSGDue, CompletionMonth, Status) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
ps.setInt(1,Integer.parseInt(t2.getText()));
ps.setString(2, t3.getText());
ps.setString(3, t4.getText());
ps.setString(4, t5.getText());
ps.setString(5, t6.getText());
ps.setString(6, t7.getText());
ps.setString(7, t8.getText());
ps.setString(8, t9.getText());
ps.setString(9, t10.getText());
ps.setString(10, t11.getText());
ps.setString(11, t12.getText());
ps.setString(12, t13.getText());
int rows = ps.executeUpdate();
if(rows>0)
{
con.close();
int sno=getMaxSNO();
t1.setText(String.valueOf(sno));
JOptionPane.showMessageDialog(null, "Data Inserted");
}
else
{
JOptionPane.showMessageDialog(null, "Failed");
}
con.close();
}
catch(Exception ae)
{
JOptionPane.showMessageDialog(null, ae.toString());
}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Name");
}
}
}
public static void main(String args[])
{
Pro obj = new Pro();
}
}
我怀疑这就是问题所在:
// Reformatted
PreparedStatement ps = con.prepareStatement(
"INSERT into project(OPANo, CollegeName, ProjectName, SanctionNoDate, " +
"TotalOutlayInLakhs, ProjectDuration, AmountReleased, " +
"BalanceToBeReleased, PRSGsHeld, NextPRSGDue, CompletionMonth, Status) " +
"VALUES(?,?,?,?,?,?,?,?,?,?,?)");
数一数问号,然后数一数你想要指定的值的个数…
(然后请注意您调用的是ps.setString(12, t13.getText())
-确认您真正的意思是有12个参数,而不是11…)