0

I am unable to execute Mysql query using datasource along with DButils This is my datasource class

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;
    }
}

This is my insert2 class

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");
    }
}

The above code is not complete but still it must not give errors as i have used it exactly as they mentioned on official page for DButils DButils site I successfully executed insert,delete and update But this resultset thing is not working(Errors I am getting in eclipse are mentioned in **)

ath j
  • 347
  • 2
  • 12
  • Your error is not clear. Can you edit and improve your question ? – davidxxx Aug 10 '16 at 18:31
  • I am unable to understand why that error is coming in the code.The code is written as mentioned on the website of DButils. – ath j Aug 11 '16 at 05:48

0 Answers0