更正:String sql="insert into table (a,b,c) values (1,2,3)";
就是将sql语句变为jdbc中prepareStatement的加问号的插值,
然后再
setString(1,object);
就是将sql语句变为jdbc中prepareStatement的加问号的插值,
然后再
setString(1,object);
stat.setString(0,1);
stat.setString(1,2);
stat.setString(2,3);
object[] s;
s[0]=1;
s[1]=2;
s[2]=3;for(int i = 0 ; i < s.length(); i ++){
stat.setString(i,s[i]);
}
将insert into (a,b,c) values (1,2,3)";处理成
insert into (a,b,c) values (?,?,?)";这种形式
然后将其参数存放到object数组中.这样就可以了
楼上的不要灌水.
将insert into (a,b,c) values (1,2,3)";处理成
insert into (a,b,c) values (?,?,?)";这种形式
然后将其参数存放到object数组中
\\\\\\\\\\\\\\\\\\\\\\\那你就写个工具方法setObj(Object[] objs)封装楼上那些代码不就行了
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection ("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=**","sa","");
String sql = "insert into (a,b,c) values (?,?,?)" ;
PreparedStatement pre = con.prepareStatement( sql ) ;
pre.setString( 1,s[0] ) ;
pre.setString( 2,s[1] ) ;
pre.setString( 3,s[2] ) ;
ResultSet rs = pre.executeQuery() ;
//其他操作省略(如:关闭等)
}
catch(SQLException e){
e.printStackTrace() ;
}
ResultSet rs = pre.executeQuery() ;为查询。
插入更新应为:pre.executeUpdata() ; 关键预查询是用到PreparedStatement 这个对象
大家都要反过来,楼主要的是吧参数传入一数组,不是从数组传进sql语句
应该是这样,
public String[] ParamInsertSql(String sql)
{
String[] myString = new String[10];
String keyValues="values";
int indexValues=sql.indexOf(keyValues);
String sql1=sql.substring(0,indexValues+keyValues.length());
String tempsql=sql.substring(indexValues+keyValues.length());
System.out.println(tempsql);
int index=tempsql.indexOf("(")+1;
int t=1;
for(int i=1;i<tempsql.length();i++)
{
char c=tempsql.charAt(i);
if(c==','||c==')')
{
myString[t]=tempsql.substring(index,i);
index=i+1;
t++;
}
}
System.out.println(t);
myString[0]=sql1+" (";
for(int i=0;i<t-2;i++)
{
myString[0]+="?,";
}
myString[0]+="?)";
return myString;
}
自己看看吧,我的意思就是这样的.但是有没有更好的写法.
insert的值不是确定的.
public class test1 {
public String[] ParamInsertSql(String sql,int count)
{
String[] myString = new String[count+1];
String keyValues="values";
int indexValues=sql.indexOf(keyValues);
String sql1=sql.substring(0,indexValues+keyValues.length());
String tempsql=sql.substring(indexValues+keyValues.length());
int index=tempsql.indexOf("(")+1;
int t=1;
for(int i=1;i<tempsql.length();i++)
{
char c=tempsql.charAt(i);
if(c==','||c==')')
{
myString[t]=tempsql.substring(index,i);
index=i+1;
t++;
}
}
myString[0]=sql1+" (";
for(int i=0;i<t-2;i++)
{
myString[0]+="?,";
}
myString[0]+="?)";
return myString;
}
public int countInsert(String sql)
{
int count=0;
String keyValues="values";
int indexValues=sql.indexOf(keyValues);
String tempsql=sql.substring(indexValues+keyValues.length());
int index=tempsql.indexOf("(")+1;
for(int i=1;i<tempsql.length();i++)
{
char c=tempsql.charAt(i);
if(c==','||c==')')
{
count++;
}
}
return count;
} public static void main(String[] args) {
test1 mytest=new test1();
String sql="insert into table (a,b,c) values (ob1,ob2,ob3)";
int count=mytest.countInsert(sql);
String[] myString= mytest.ParamInsertSql(sql,count);
for(int i=0;i<myString.length;i++)
{
System.out.println("myString["+i+"]="+myString[i]+"|");
}
}
}但是要是update的sql语句该如何处理呢?
public String[] ParamInsertSql(String sql,int count)
{
//sql=sql.toLowerCase();
String[] myString = new String[count+1];
String keyValues="values",keyWhere="where",keyAnd="and";
int indexValues=sql.indexOf(keyValues);
String sql1=sql.substring(0,indexValues+keyValues.length());
String tempsql=sql.substring(indexValues+keyValues.length());
int index=tempsql.indexOf("(")+1;
int t=1;//计算myString 的插值位置.
for(int i=1;i<tempsql.length();i++)
{
char c=tempsql.charAt(i);
if(c==','||c==')')
{
myString[t]=tempsql.substring(index,i);
index=i+1;
t++;
}
}
myString[0]=sql1+" (";
for(int i=0;i<t-2;i++)
{
myString[0]+="?,";
}
myString[0]+="?)";
int indexWhere=sql.indexOf(keyWhere);
if(indexWhere>0)
{
String sql2=sql.substring(indexWhere);
int indexAnd=serchAnd(sql2);
if(indexAnd>0)
{
String sql3=sql2.substring(0,sql2.indexOf(keyAnd));
for(int i=1;i<sql3.length();i++)
{
char c=sql3.charAt(i);
if(c=='='||c=='<'||c=='>')
{
myString[t]=sql3.substring(i+1);
sql3=sql3.substring(0,i);
myString[0]+=" "+sql3+c+"?";
System.out.println("sql3="+sql3);
}
}
sql3=sql2.substring(sql2.indexOf(keyAnd));
//System.out.println(sql3);
int offset=0;
for(int i=1;i<sql3.length()&&indexAnd>0;i++)
{
char c=sql3.charAt(i);
if(c=='='||c=='<'||c=='>')
{
indexAnd--;
String sql4=sql3.substring(i);
System.out.println(sql4);
String sql5=sql3.substring(offset,i);
int index1=sql4.indexOf(keyAnd);
offset=i+index1;
if(index1>0)
{
myString[++t]=sql3.substring(i+1,index1+i);
}
else
{
myString[++t]=sql3.substring(i+1);
}
char d=sql3.charAt(i+1);
if(c=='<'&&d=='>')
{
myString[0]+=" "+sql5+c+d+"?";
i++;
}
else
{
myString[0]+=" "+sql5+c+"?";
}
}
}
}
else
{
for(int i=1;i<sql2.length();i++)
{
char c=sql2.charAt(i);
if(c=='='||c=='<'||c=='>')
{
myString[t]=sql2.substring(i+1);
sql2=sql2.substring(0,i);
char d=sql2.charAt(i+1);
if(c=='<'&&d=='>')
{
myString[0]+=" "+sql2+c+d+"?";
i++;
}
else
myString[0]+=" "+sql2+c+"?";
break;
}
}
}
}
return myString;
}
public int countInsert(String sql)
{
int count=0;
String keyValues="values",keyWhere="where",keyAnd="and";
int indexValues=sql.indexOf(keyValues);
String tempsql=sql.substring(indexValues+keyValues.length());
int index=tempsql.indexOf("(")+1;
for(int i=1;i<tempsql.length();i++)
{
char c=tempsql.charAt(i);
if(c==','||c==')')
{
count++;
}
}
int indexWhere=sql.indexOf(keyWhere);
if(indexWhere>0)
{
String sql2=sql.substring(indexWhere);
int indexAnd=serchAnd(sql2);
if(indexAnd>0)
{ String sql3=sql2.substring(0,sql2.indexOf(keyAnd));
for(int i=1;i<sql3.length();i++)
{
char c=sql3.charAt(i);
if(c=='='||c=='<'||c=='>')
{
count++;
sql3=sql3.substring(0,i+1);
}
}
sql3=sql2.substring(sql2.indexOf(keyAnd));
//System.out.println(sql3);
for(int i=1;i<sql3.length()&&indexAnd>0;i++)
{
char c=sql3.charAt(i);
if(c=='='||c=='<'||c=='>')
{
indexAnd--;
count++;
}
}
}
else
{
for(int i=1;i<sql2.length();i++)
{
char c=sql2.charAt(i);
if(c=='='||c=='<'||c=='>')
{
count++;
break;
}
}
}
}
return count;
}
/*
* 根据sql语句,来查找and的个数.
*
*
*/
public int serchAnd(String sql)
{
String key="and";
int index=sql.indexOf(key);
int countAnd=0;
if(index>0)
{
countAnd++;
String tmpSql=sql.substring(index+key.length());
countAnd=countAnd+serchAnd(tmpSql);
}
return countAnd;
} public static void main(String[] args) {
test1 mytest=new test1();
String sql="insert into table (a,b,c) values (ob1,ob2,ob3) where dev_code<>'13010121'and dev_type=100000 and dev_class=121212 and class<>222222 and dev=1000";
int count=mytest.countInsert(sql);
String[] myString= mytest.ParamInsertSql(sql,count);
for(int i=0;i<myString.length;i++)
{
System.out.println("myString["+i+"]="+myString[i]+"|");
} }
}