无法使用DButils和Datasource执行Mysql查询

时间:2016-08-10 18:27:07

标签: java eclipse jdbc datasource apache-commons-dbutils

我无法使用数据源和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 site 我成功执行了插入,删除和更新 但是这个结果集的东西不起作用(我在日食中得到的错误在**中提到)

0 个答案:

没有答案