1
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)
4

1 回答 1

1

您应该使用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();
        }
    }
}
于 2012-09-12T15:52:41.163 回答