我无法使用数据源和 DButils 执行 Mysql 查询这是我的数据源类
package com.log.in;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class datasource {
public static DataSource getMySQLDataSource() throws SQLException
{
Properties props = new Properties();
FileInputStream fis = null;
MysqlDataSource mysqlDS = null;
try
{
fis = new FileInputStream("ext/db.properties");
props.load(fis);
mysqlDS = new MysqlDataSource();
mysqlDS.setURL(props.getProperty("MYSQL_DB_URL"));
mysqlDS.setUser(props.getProperty("MYSQL_DB_USERNAME"));
mysqlDS.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
} catch (IOException e)
{
e.printStackTrace();
}
return mysqlDS;
}
}
这是我的 insert2 类
package com.log.in;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.*;
import com.mysql.jdbc.ResultSetMetaData;
public class Insert2 {
public static void main(String[] args)
{
try {
fetchDataSource();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void fetchDataSource() throws SQLException
{
DataSource ds = null;
try
{
ds = datasource.getMySQLDataSource();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
ResultSetHandler<Object[]> obj = new ResultSetHandler<Object[]>()
{
ResultSet rs = null;
@Override
public Object[] handle(ResultSet rs) throws SQLException
{
return null;
}
com.mysql.jdbc.ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();
int cols = meta.getColumnCount();
Object[] result = new Object[cols];**Syntax error, insert ";" to complete LocalVariableDeclarationStatement**
for(int i=0;i<cols;i++)
{
result[i] = rs.getObject(i+1);
}
return result;
};**Here it shows Syntax error on token "}", delete this token**
// Create a QueryRunner that will use connections from
// the given DataSource
QueryRunner run = new QueryRunner(dataSource);
// Execute the query and get the results back from the handler
Object[] result = run.query(
"SELECT * FROM Person WHERE name=?", h, "John Doe");
}
}
上面的代码不完整,但它仍然不能给出错误,因为我已经完全按照他们在 DButils DButils 网站的官方页面上提到的那样使用它 我成功执行了插入、删除和更新但是这个结果集的东西不起作用(我遇到的错误日食在**中提到)