gpt4 book ai didi

java - 如何将sql查询数组传递给函数

转载 作者:行者123 更新时间:2023-11-29 08:04:41 26 4
gpt4 key购买 nike

代码有什么问题。请帮我解决这个问题。如何将查询数组传递给函数 exeSearchQuery();

  public class  MyDBConnect
{
Connection dbCon = null;
public String DB_USERNAME = "root";
public String DB_PASSWORD = "root";
private String DB_DRIVER_CLASS = "com.mysql.jdbc.Driver";
private String DB_URL = "jdbc:mysql://localhost/yatin";
public boolean connectToDb( ) throws Exception{
try{
Class.forName (DB_DRIVER_CLASS);
dbCon = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
//dbCon = DriverManager.getConnection(tns, DB_USERNAME, DB_PASSWORD);
return true;
}catch(Exception e){
System.out.println("Error in DBConnect "+e);
return false;
}
}

public ArrayList exeSearchQuery(String sql[]) throws Exception{

Statement stmt = null;
ResultSet rs = null;
ArrayList data = null;
int recordSize=0;
try{

connectToDb( );
stmt = dbCon.createStatement( );
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int numOfCols = rsmd.getColumnCount();

data = new ArrayList( );
String q="";
for(int k=0;k<2;k++)
{
boolean chkQuery=true;
q=sql[k];
while(rs.next()){
chkQuery=true;
recordSize=recordSize+1;
String row[] = new String[numOfCols];
for(int col=1;col<=numOfCols ;col++)
{
row[col-1] = rs.getString(col);
}
data.add(row);
}
System.out.println("data="+data);

if(chkQuery=true)
{
break;
}
else
{
q=sql[1];
while(rs.next())
{
chkQuery=true;
recordSize=recordSize+1;
String row[] = new String[numOfCols];
for(int col=1;col<=numOfCols ;col++)
{
row[col-1] = rs.getString(col);
}
data.add(row);
System.out.println("data="+data);
}
}
if(chkQuery=true)
{
break;
}
}

}catch(Exception e){
System.out.println("Error in DBConnect "+e);
}finally{
if(rs !=null) rs.close();
if(stmt !=null)stmt.close();
if(dbCon !=null) dbCon.close();
}
return data;
//return recordSize;
}

public static void main(String a[]) throws Exception{
MyDBConnect db = new MyDBConnect( );
//db.connectToDb();
System.out.println("Connected");
String query[] ={"select a.ID,a.VDT,a.AID,a.NAME,a.ADD,a.CONTACT,a.PTM,a.DEPT_TEXT,a.FLOOR,b.LINK from tm_co123 a LEFT JOIN tm_co456 b ON a.ID=b.PID where a.NAME like 'anil' ORDER BY ID","select a.ID,a.VDT,a.AID,a.NAME,a.ADD,a.CONTACT,a.PTM,a.DEPT_TEXT,b.LINK from tm_co123 a LEFT JOIN tm_co456 b ON a.ID=b.PID where a.ADD like 'anil' ORDER BY ID"};
db.exeSearchQuery(query[]);
}
}

如果我运行上面的代码,我会得到 Error as

MyDBConnect.java:473: '.class' expected
db.exeSearchQuery(query[]);
^
1 error

如果我用 query 替换 query[] 那么它会给我错误 as

MyDBConnect.java:406: executeQuery(java.lang.String) in java.sql.Statement canno
t be applied to (java.lang.String[])
rs = stmt.executeQuery(sql);
^
Note: MyDBConnect.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

最佳答案

您不能将 String[] 传递给 executeQuery ,它需要一个 String 作为参数。分别执行每个查询,或者使用 UNION 将它们变成一个查询。

关于java - 如何将sql查询数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12070530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com