用PreparedStatment ps =conn.prePareStatment(select isnull(count(*),0)+1,?,? from B where B=?"); ps.setString(1,"b");//注意ps.这里的数据类型与数据库表,和"b" 的类型想对应 如:ps.setInt(2,3) 如果用StringBuffer : StringBuffer sql=new StringBuffer(); sql.append("select isnull(count(*),)) +1 , ");//注意空格 sql.append(?);//你要的查询的 sql.append(" , "); 用的时候sql.toString() 就可以了
用String: String sql="insert into AAA(A,B,C) select isnull(count(*),0)+1,"+ 参数1+","+参数2+" from B where B='"+参数三+"'";用StringBuffer: StringBuffer sql = new StringBuffer(); sql.append(="insert into AAA(A,B,C) select isnull(count(*),0)+1,").append(参数1) .append(",").append(参数2).append( " from B where B = ").append(参数3); 或者(如果参数3即B的为字符串类型) StringBuffer sql = new StringBuffer(); sql.append(="insert into AAA(A,B,C) select isnull(count(*),0)+1,").append(参数1) .append(",").append(参数2).append( " from B where B = '").append(参数3).append("'");
ps.setString(1,"b");//注意ps.这里的数据类型与数据库表,和"b" 的类型想对应
如:ps.setInt(2,3)
如果用StringBuffer :
StringBuffer sql=new StringBuffer();
sql.append("select isnull(count(*),)) +1 , ");//注意空格
sql.append(?);//你要的查询的
sql.append(" , ");
用的时候sql.toString() 就可以了
String sql="insert into AAA(A,B,C)
select isnull(count(*),0)+1,"+ 参数1+","+参数2+" from B where B='"+参数三+"'";用StringBuffer:
StringBuffer sql = new StringBuffer();
sql.append(="insert into AAA(A,B,C) select isnull(count(*),0)+1,").append(参数1)
.append(",").append(参数2).append( " from B where B = ").append(参数3);
或者(如果参数3即B的为字符串类型)
StringBuffer sql = new StringBuffer();
sql.append(="insert into AAA(A,B,C) select isnull(count(*),0)+1,").append(参数1)
.append(",").append(参数2).append( " from B where B = '").append(参数3).append("'");
sql.append("insert into AAA(A,B,C) ")没有"="号.