代码有什么问题。请帮助我。如何将查询数组传递给函数 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[]);
}
}
如果我运行上面的代码,我会收到错误
MyDBConnect.java:473: '.class' expected
db.exeSearchQuery(query[]);
^
1 error
如果我用 query 替换 query[] 那么它会给我错误
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