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.