java.sql.SQLException: ORA-01006: 绑定变量不存在
java.sql.SQLException: ORA-01006: 绑定变量不存在
这是什么异常啊
------最佳解决方案--------------------你是在java里面调用Oracle的存储过程吗?
如果是的话,你可能写错了绑定变量,你用单引号将绑定变量给引住了?
绑定变量是一个整体的东西,不需要用单引号,用了单引号就不是绑定变量了
------其他解决方案--------------------rs = pstam.executeQuery("select * from dept");
pstam是一个preparedStatement,不能这么用。因为它要求要绑定变量,所以出现你说的错。
在oracle jdbc中,你得定义一个新的statement
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
------其他解决方案--------------------这个程序
import java.sql.*;
import java.util.*;
public class TestPreStat {
public static void main(String[] agrs){
Scanner sc = new Scanner(System.in);
Connection conn = null;
PreparedStatement pstam = null;
ResultSet rs = null;
int deptno = 0;
String dname = "";
String loc = "";
System.out.println("输入部门编号:");
deptno = sc.nextInt();
System.out.println("输入部门名称:");
dname = sc.next();
System.out.println("输入部门地址:");
loc = sc.next();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
pstam = conn.prepareStatement("insert into dept values (?,?,?)");
pstam.setInt(1,deptno);
pstam.setString(2, dname);
pstam.setString(3, loc);
pstam.executeUpdate();
rs = pstam.executeQuery("select * from dept");
while(rs.next()){
System.out.println(rs.getString("deptno")+"\t"+rs.getString("dname")+"\t"+rs.getString("loc"));
}
} catch (ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
} finally {
try{
if(rs != null){
rs.close();
}
if(pstam != null){
pstam.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e){
e.printStackTrace();
}
}
}
}