-2

I'm trying to make a Java application that can access a MySQL database hosted there and send SELECT and UPDATE statements to a specific table.

My current connection code is looking like this:

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("username");          -- line 1
dataSource.setPassword("password");      -- line 2
dataSource.setURL("db url");             -- line 3

However the bottom three lines are throwing these errors in Eclipse:

Line 1 and 2:

- Syntax error on token ";", @ expected
- Syntax error on token ".", @ expected after

Line 3:

- Syntax error, insert "SimpleName" to complete 
 QualifiedName
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "Identifier (" to complete 
 MethodHeaderName

I looked into the source code for MysqlDataSource, and setUser is definitely public (as well as setPassword and setServerName). I have no idea why it's causing an issue.

Werns
  • 19
  • 3
  • 1
    Just to clarify, does your file contain exactly these 6 lines of code? – Andremoniy May 09 '16 at 22:27
  • 1
    If this is a compile error, please paste the entire contents of the file containing your connection code. Based on the snippet, you may not have declared a class, and the compiler may be expecting an annotation, but it's hard to be sure without seeing the code. If it's a runtime error, please provide the entire stack trace. – Warren Dew May 09 '16 at 22:33
  • Please only ask one question per post. We don't need to know what university you attend. If you think a question is incorrectly closed a duplicate, do [this](http://meta.stackoverflow.com/questions/253521/what-can-i-do-if-i-believe-that-my-question-was-wrongly-marked-as-a-duplicate). You also don't need to mention your experience with those in new posts. – Sotirios Delimanolis May 09 '16 at 22:42
  • Andremoniy - The string values have been changed, but otherwise that is copy and pasted code. WarrenDew - It's a compile error, but none of the rest of the code has anything to do with this. The rest is GUI creation. In order for the application to do anything, I need to be able to check the tables. – Werns May 11 '16 at 13:52

1 Answers1

0

Executable code must be wrapped in a class block. javac is expecting either an annotation such as @Entity (from javax.persistence) or a class declaration. Based on the code above, your code should look like:

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

class DBCheck {
    private MysqlDataSource dataSource;

    public DBCheck() {
        dataSource = new MysqlDataSource();
    }

    public Connection connect() throws SQLException {
        dataSource.setUser("username");
        dataSource.setPassword("password");
        dataSource.setURL("db url");

        return dataSource.getConnection();
    }

    public static main( String[] args ) {
        Connection conn = null;
        try {
            DBCheck dbc = new DBCheck();
            conn = dbc.connect();
        } catch( SQLException e ) {
            // code to handle exception
        } finally {
            if( conn != null ) {
                conn.close();
            }
        }
    }
}

For a better explanation of using a DataSource without JNDI see this short tutorial

walsht
  • 196
  • 1
  • 10