日期:2014-05-17  浏览次数:20739 次

有何区别呢???

conn=JdbcUtils.getConnection();
String sql="insert into users(id,username,password,email,birthday) values(?,?,?,?,?)";
st=conn.prepareStatement(sql);
st.setString(1,user.getId());
st.setString(2,user.getUsername());
st.setString(3, user.getPassword());
st.setString(4,user.getEmail());
st.setDate(5,new java.sql.Date(user.getBirthday().getTime()));

st.executeUpdate();




这里的executeUpdate()方法,括号里写与不写sql有何区别呢??我的一个程序,不写的时候能正常运行,写了就运行报错了。原问题在:http://bbs.csdn.net/topics/390613427
sql java string

------解决方案--------------------
错误的代码怎么不贴出来??
我觉得可能是这样st=conn.prepareStatement(sql);,这里已经预编译一次sql语句了,然后下面executeUpdate在传递一次 等于就是重复了,,你不信可以把上面预编译的sql去掉,然后下面executeUpdate的时候传递sql试试看不就知道了,如果不行那就直接f3调试看看源代码。。
------解决方案--------------------
executeUpdate

int executeUpdate(String sql)
                  throws SQLException
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
Parameters:
sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.
Returns:
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
Throws:
SQLException - if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces a ResultSet object


==================================================================

executeQuery

ResultSet executeQuery()
                       throws SQLException
Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
Returns:
a ResultSet object that contains the data produced by the query; never null
Throws:
SQLException - if a database access error occurs; this method i