并非所有变量都已绑定

并非所有变量都已绑定

import java.sql.*;

class TestConnection
{
public static void main(String args[])
{

    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:ram","myuser","myuser");

        Statement st = con.createStatement();
        st.executeUpdate("insert into student values( &stno, &sname, &course, &fees); ");
        System.out.println("1 row Inserted");

        con.close();
    }
    catch(Exception P)
    {
        P.printStackTrace();
    }
}


}

我已经创建了表格,我想从键盘输入输入,但是出现以下错误,请帮助我......。

java.sql.SQLException: [Oracle][ODBC][Ora]ORA-01008: not all variables bound
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
at TestConnection.main(TestConnection.java:13)

最佳答案

您应该使用PreparedStatement并按如下所示设置参数:

import java.sql.*;

class TestConnection
{
    public static void main(String args[])
    {
        int stno = Integer.parseInt(args[0]);
        String name = args[1];
        String course = args[2];
        double fees = Double.parseDouble(args[3]);

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con =
                DriverManager.getConnection("jdbc:odbc:ram","myuser","myuser");
            PreparedStatement st =
                con.prepareStatement("insert into student values(?, ?, ?, ?)");
            st.setInt(1, stno);
            st.setString(2, name);
            st.setString(3, course);
            st.setDouble(4, fees);
            // executeUpdate returns the number of rows inserted/updated/deleted.
            int count = st.executeUpdate();
            System.out.println(count + " row(s) inserted.");
            con.close();
        }
        catch(Exception P)
        {
            P.printStackTrace();
        }
    }
}

关于java - java.sql.SQLException:[Oracle] [ODBC] [Ora] ORA-01008:并非所有变量都已绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12391965/

10-10 01:39