当前位置: 技术问答>java相关
急问题:在java中嵌入sql的插入语句,插入成功,但是出现异常
来源: 互联网 发布时间:2015-03-17
本文导语: 下面是一个程序,编译时没有错误, 运行结果也对(在表Tablepl中插入了数据'123','name') 但是运行后却打出sql异常:SQL Error:java.sql.SQLException:No ResultSet was produced 0 null 问题解决不了,还请高手指教,谢谢! import ...
下面是一个程序,编译时没有错误,
运行结果也对(在表Tablepl中插入了数据'123','name')
但是运行后却打出sql异常:SQL Error:java.sql.SQLException:No ResultSet was produced 0 null
问题解决不了,还请高手指教,谢谢!
import java.sql.*;
public class President {
public static void main(String[] arguments) {
String data = "jdbc:odbc:prc_c"; //odbc-jdbc桥
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "sa", "");
Statement st = conn.createStatement();
System.out.println("插入");
String s="insert Tablepl values ('123','name')"; //sql插入语句
ResultSet rec = st.executeQuery(s);
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}
}
}
运行结果也对(在表Tablepl中插入了数据'123','name')
但是运行后却打出sql异常:SQL Error:java.sql.SQLException:No ResultSet was produced 0 null
问题解决不了,还请高手指教,谢谢!
import java.sql.*;
public class President {
public static void main(String[] arguments) {
String data = "jdbc:odbc:prc_c"; //odbc-jdbc桥
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "sa", "");
Statement st = conn.createStatement();
System.out.println("插入");
String s="insert Tablepl values ('123','name')"; //sql插入语句
ResultSet rec = st.executeQuery(s);
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}
}
}
|
public class President {
public static void main(String[] arguments) {
String data = "jdbc:odbc:prc_c"; //odbc-jdbc桥
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "sa", "");
PreparedStatement pstm =
conn.prepareStatement(
"INSERT INTO Tablepl( ?,?) VALUES ( ?, ?)");
pstm.executeUpdate();
pstm.close();
}catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
}
}
我的机器上没装SQL Server 所以没法帮你测试,试试上面的代码。
public static void main(String[] arguments) {
String data = "jdbc:odbc:prc_c"; //odbc-jdbc桥
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "sa", "");
PreparedStatement pstm =
conn.prepareStatement(
"INSERT INTO Tablepl( ?,?) VALUES ( ?, ?)");
pstm.executeUpdate();
pstm.close();
}catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
}
}
我的机器上没装SQL Server 所以没法帮你测试,试试上面的代码。
|
问题:
String s="insert Tablepl values ('123','name')"; //sql插入语句
ResultSet rec = st.executeQuery(s);
解决分析:
sql语句:
1、INSERT, UPDATE or DELETE statements 语句:
执行:Connection.createStatement.executeUpdate(String s);
2、SELECT statements
执行:Connection.createStatement.executeQuery(String s);
3、 s="insert Tablepl values ('123','name')"; //sql插入语句
最好写成:s="INSERT INTO Tablepl values ('123','name')";
String s="insert Tablepl values ('123','name')"; //sql插入语句
ResultSet rec = st.executeQuery(s);
解决分析:
sql语句:
1、INSERT, UPDATE or DELETE statements 语句:
执行:Connection.createStatement.executeUpdate(String s);
2、SELECT statements
执行:Connection.createStatement.executeQuery(String s);
3、 s="insert Tablepl values ('123','name')"; //sql插入语句
最好写成:s="INSERT INTO Tablepl values ('123','name')";
|
insert语句并不返回值,请不要附给别的变量,这种错误我以前碰到过,只要单独写:
st.executeUpdate(s);
就ok了,对了,楼上说的很对,应该用executeUpdate
st.executeUpdate(s);
就ok了,对了,楼上说的很对,应该用executeUpdate
|
听了楼上的话,有启发。
|
是因为INSERT语句的结果不是结果集,而是INSERT作用的行数,如果用int i = st.executeQuery(s); 应可以
|
对insert,update,delete请不要用executeQuery(),而要用executeUpdate()