概述
publicintexeSql(Stringsql,Object[]param){intsign=0;PreparedStatementps=null;Connectionconn=null;try{conn=this.getConn();ps=conn.prepareStatement(sql);if(param!=null&¶...
public int exeSql(String sql,Object[] param)
{
int sign=0;
PreparedStatement ps=null;
Connection conn=null;
try {
conn=this.getConn();
ps=conn.prepareStatement(sql);
if (param!=null&¶m.length>0) {
for (int i = 0; i < param.length; i++) {
ps.setObject(i+1, param[i]);
}
}
sign=ps.executeUpdate(sql);
System.out.println("执行sql成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
this.closeAll(null, ps, conn);
}
return sign;
}
这是我DBManager中的代码 测试是我调用时
public static void main(String[] args) {
// TODO Auto-generated method stub
BaseDao bdao=new BaseDao();
bdao.exeSql("insert into logininfo values('?','?')", new Object[]{"admin","admin"});
}
提示语法错误 可是明明没错啊
错误提示
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2822)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1536)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1626)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3025)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1147)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1070)
at com.base.BaseDao.exeSql(BaseDao.java:41)
at com.test.TestDB.main(TestDB.java:14)
是不是mysql的版本有问题啊
展开
最后
以上就是寂寞彩虹为你收集整理的mysql+prepare+statement_java连mysql是执行查询是能用PrepareStatement类吗的全部内容,希望文章能够帮你解决mysql+prepare+statement_java连mysql是执行查询是能用PrepareStatement类吗所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复