java.sql.SQLException错误

运行StuDialog.class出现下面错误提示,初学Java请求帮助找出下面是什么原因?
java.sql.SQLException: No value specified for parameter 2
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2560)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2536)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2383)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
at com.studentmanager.StuAddDialog.actionPerformed(StuAddDialog.java:109)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1051)
at java.awt.Dialog$3.run(Dialog.java:1103)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1101)
at java.awt.Component.show(Component.java:1516)
at java.awt.Component.setVisible(Component.java:1468)
at java.awt.Window.setVisible(Window.java:841)
at java.awt.Dialog.setVisible(Dialog.java:991)
at com.studentmanager.StuAddDialog.(StuAddDialog.java:73)
at com.studentmanager.StuDialog.actionPerformed(StuDialog.java:109)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

1、StuDialog.class如下:
package com.studentmanager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
public class StuDialog extends JFrame implements ActionListener{

//定义一些控件
JPanel jp1,jp2;
JLabel jl1;
JButton jb1,jb2,jb3,jb4;
JTable jt;
JScrollPane jsp;
JTextField jtf;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    StuDialog stuDialog=new StuDialog();

}
public StuDialog()
{
    jp1=new JPanel();
    jtf=new JTextField(10);
    jb1=new JButton("查询");
    jb1.addActionListener(this);
    jl1=new JLabel("请输入名字");

    //把各个控件加入到jp1
    jp1.add(jl1);
    jp1.add(jtf);
    jp1.add(jb1);

    jp2=new JPanel();

    jb2=new JButton("添加");
    jb2.addActionListener(this);
    jb3=new JButton("修改");
    jb4=new JButton("删除");

    //把各个按钮就加入到jp2
    jp2.add(jb2);
    jp2.add(jb3);
    jp2.add(jb4);


    /*//rowData可以存放多行
    Vector hang=new Vector();
    hang.add("sp001");
    hang.add("孙悟空");
    hang.add("男");
    hang.add("25");
    hang.add("火锅山");
    hang.add("少林派");

    //加入到rowData
    rowData.add(hang);*/

    //创建一个数据模型对象
    StuModel sm=new StuModel();

    //初始化JTable
    jt=new JTable(sm);

    //初始化Jsp JScrollPane
    jsp=new JScrollPane(jt);

    //把jsp放入JFrame
    this.add(jsp);
    this.add(jp1,"North");
    this.add(jp2,"South");
    this.setSize(400,300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);



}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    //判断是哪个按钮被点击
    if(e.getSource()==jb1)
    {
        System.out.println("用户想查询");
        //因为把表的现实的数据封装到StuModel中,那么我们就可以
        //简单完成查询
        //查询数据库,更新JTable

        String name=this.jtf.getText().trim();
        //写一个SQL语句
        String sql="select * from stu where stuName='"+name+"'";
        //构建新的数据模型,并更新
        StuModel sm=new StuModel(sql);
        //更新JTable
        jt.setModel(sm);

        //当用户点击添加

    }
    else if(e.getSource()==jb2)
    {
        StuAddDialog sa=new StuAddDialog(this,"添加学生",true);
        //重新再获得新的数据模型
    }
}

}

2、StuAddDialog.class如下:
package com.studentmanager;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.JDialog;
import java.sql.*;
public class StuAddDialog extends JDialog implements ActionListener {

    //定义我需要的swing控件
JPanel jp1,jp2,jp3;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
JButton jb1,jb2;
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
//owner 它的父窗口
//title窗口名
//model指定是模式窗口,还是非模式窗口
public StuAddDialog(Frame owner,String title,boolean modal)
{
    super(owner,title,modal);//调用父类构造方法,达到模式对话框效果
    jl1=new JLabel("学号");
    jl2=new JLabel("姓名");
    jl3=new JLabel("性别");
    jl4=new JLabel("年龄");
    jl5=new JLabel("籍贯");
    jl6=new JLabel("系别");

    jtf1=new JTextField(); 
    jtf2=new JTextField();
    jtf3=new JTextField();
    jtf4=new JTextField();
    jtf5=new JTextField();
    jtf6=new JTextField();

    jb1=new JButton("添加");
    jb1.addActionListener(this);
    jb2=new JButton("取消");

    jp1=new JPanel();
    jp2=new JPanel();
    jp3=new JPanel();

    //设置布局
    jp1.setLayout(new GridLayout(6,1));
    jp2.setLayout(new GridLayout(6,1));

    jp1.add(jl1);
    jp1.add(jl2);
    jp1.add(jl3);
    jp1.add(jl4);
    jp1.add(jl5);
    jp1.add(jl6);

    jp2.add(jtf1);
    jp2.add(jtf2);
    jp2.add(jtf3);
    jp2.add(jtf4);
    jp2.add(jtf5);
    jp2.add(jtf6);

    jp3.add(jb1);
    jp3.add(jb2);

    this.add(jp1,BorderLayout.WEST);
    this.add(jp2,BorderLayout.CENTER);
    this.add(jp3,BorderLayout.SOUTH);

    jb1.addActionListener(this);

    //展现
    this.setSize(300,250);
    //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);


}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==jb1)
    {
        //对用户点击添加按钮后的响应动作
        //连接数据库
        Connection ct=null;
        Statement stmt=null;
        ResultSet rs=null;
        PreparedStatement psmt=null;
        //连接数据库,判断用户是否合法
        try{
            //1加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2创建连接对象,注册驱动程序并打开连接对象
            ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=GBK","root","123456");
            psmt=ct.prepareStatement("select * from stu");
            //3获取语句对象
            //stmt=ct.createStatement();
            //与编译语句对象
            psmt=ct.prepareStatement("insert into stu values(?,?,?,?,?,?)");

            //给参数赋值
            psmt.setString(1,jtf1.getText());
            psmt.setString(1,jtf2.getText());
            psmt.setString(1,jtf3.getText());
            psmt.setString(1,jtf4.getText());
            psmt.setString(1,jtf5.getText());
            psmt.setString(1,jtf6.getText());

            //4执行操作
            psmt.executeUpdate();

            this.dispose();//关闭对话框



        }catch(ClassNotFoundException e1)
        {
            e1.printStackTrace();
        }catch(SQLException e2)
        {
            e2.printStackTrace();
        }
        finally
        {
            //释放语句对象,连接对象
            try
            {
                if(rs!=null) rs.close();
                if(stmt!=null) stmt.close();
                if(ct!=null) ct.close();

            }catch(Exception e2)
            {
                e2.printStackTrace();
            }
        }

    }
}

@Override
public Container getContentPane() {
    // TODO Auto-generated method stub
    return super.getContentPane();
}

@Override
public void setContentPane(Container contentPane) {
    // TODO Auto-generated method stub
    super.setContentPane(contentPane);
}

}

3个回答

[code="java"]
//与编译语句对象
psmt=ct.prepareStatement("insert into stu values(?,?,?,?,?,?)");

//给参数赋值
psmt.setString(1,jtf1.getText());
psmt.setString(1,jtf2.getText());
psmt.setString(1,jtf3.getText());
psmt.setString(1,jtf4.getText());
psmt.setString(1,jtf5.getText());
psmt.setString(1,jtf6.getText());
[/code]
很明显的,你在考验我的视力,下标全是1,那后面的全都为空了,所以他会说第二个参数没有值呀! :oops:

[code="java"]
//给参数赋值
psmt.setString(1,jtf1.getText());
psmt.setString(1,jtf2.getText());
psmt.setString(1,jtf3.getText());
psmt.setString(1,jtf4.getText());
psmt.setString(1,jtf5.getText());
psmt.setString(1,jtf6.getText());
[/code]
what happend?为什么下标全是1?

:P 呵呵,当局者迷,旁观者清呀,写代码久了就这样呀,我这段时间也老犯糊涂,所以就想上这里找个比我更晕胡的,呵呵,找到了,就你了 :lol:

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问