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的版本有问题啊
java连mysql是执行查询是能用PrepareStatement类吗
答案:4 悬赏:70 手机版
解决时间 2021-02-11 22:03
- 提问者网友:温旧梦泪无声
- 2021-02-11 02:05
最佳答案
- 五星知识达人网友:躲不过心动
- 2021-02-11 03:37
应该可以用的 如果配置没有问题的话试试setString
然后insert into logininfo values(?,?)
然后insert into logininfo values(?,?)
全部回答
- 1楼网友:渊鱼
- 2021-02-11 07:23
PrepareStatement是接口,不是类
- 2楼网友:过活
- 2021-02-11 06:00
系统提示你错误,很明显就有错麻~~~
错误贴出来~
- 3楼网友:爱难随人意
- 2021-02-11 05:03
string sql="insert into pic values(1,'pic1',?)";
preparedstatement pstm=conn.preparestatement(sql);
blob blob=blob.getemptyblob();
pstm.setblob(1, blob);
pstm.executeupdate();
pstm.close();
sql="select * from pic where pic_id=1 for update";
pstm=conn.preparestatement(sql);
resultset rs=pstm.executequery();
if(rs.next()){
blob b=rs.getblob("pic_data");
outputstream out=b.setbinarystream(1);
inputstream in =new fileinputstream("j:/pic.jpg");
int k=-1;
while((k=in.read())!=-1){
out.write(k);
}
out.flush();
out.close();
in.close();
}
conn.close();
rs.close();
pstm.close();
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯