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)
答案 0 :(得分: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();
}
}
}