-1
package com.sample;

import java.sql.DriverManager;
import com.mysql.jdbc.Connection;

public class connectionclass {
    public static void main(String args) {
        System.out.println("MySql Connect Example");
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "testdatabase";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "root";
        try {
            Class.forName(driver).newInstance();
            conn = (Connection) DriverManager.getConnection(url + dbName,
                    userName, password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code connects to a database and gives no syntax error. I am doing this in Eclipse and when I run the project it asks which class we need to run, but my class is not there.

Taj Morton
  • 1,588
  • 4
  • 18
  • 26
user2782773
  • 99
  • 2
  • 5
  • 12

2 Answers2

3

Please correct this line

public static void main(String[] args) 
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
Jayaram
  • 1,715
  • 18
  • 30
  • Launch configuration DocsConnectionPropsHelper references non-existing project connection. Error comes when i run it DocsConnectionProperHelper – user2782773 Sep 18 '13 at 07:02
  • @user2782773: Sorry didn't get what you want to do...as per my understanding from the above, you have a main method inside the class connectionclass, so right click, slect as java application, it will execute..between please make your class name starts with capital character – Jayaram Sep 18 '13 at 07:06
  • Access denied for user 'root'@'localhost' (using password: YES) Error coming now – user2782773 Sep 18 '13 at 07:07
  • @user2782773, yes this is another issue, when you are trying to get the connection i.e this line:conn = (Connection) DriverManager.getConnection(url + dbName, userName, password);, as from the exception, Please try to login to your database with the parameters, it might be you have wrong password, or password is not set for that – Jayaram Sep 18 '13 at 07:09
  • @user2782773: http://stackoverflow.com/questions/6081339/access-denied-for-user-rootlocalhost-using-password-yes-mysqlerror check out this answer, for the access denied issue – Jayaram Sep 18 '13 at 07:11
1

main method accepts arguments of String array so do this way

public static void main(String args[])
SpringLearner
  • 13,738
  • 20
  • 78
  • 116