Alon Shmiel
Alon Shmiel

Reputation: 7121

connection between jdbc to mysql

I have the next code. I save it in the file: 'MysqlConnect.java'.

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "root";
    try {
        Class.forName(driver).newInstance();
        conn = 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(); 
    }
  }
}

I know how to run it:

javac MysqlConnect.java
java MysqlConnect

I have some questions:

1) where should I save this file? (I have a folder of: mysql-connector-java-5.1.22).

2) I created a database and a table in mysql. I don't need to insert that into the code? for example:

String dbname = "database_alon";

3) If the answer of the second question is 'YES', I only have a password to my database, not an username. so what should I insert to the parameter of userName?

so, I think that I have to change the code to:

String dbName = "database_alon";
String driver = "com.mysql.jdbc.Driver";
String userName = ""; 
String password = "ADMINALON";

right? and what about the question 1?

Upvotes: 2

Views: 702

Answers (1)

Konstantinos Margaritis
Konstantinos Margaritis

Reputation: 3247

You can have your MysqlConnect.java file wherever you want. You should just add the folder containg the MysqlConnect.class in your classpath. Moreover, in order to run properly you should also add the mysql-connector-java-5.1.22.jar to the classpath in order to be able to find the classes you require for running your class.

In order to avoid mutiple additions in the classpath you could add the mysql-connector-java-5.1.22.jar in the folder containing the MysqlConnect.class and make only one update of the classpath.

As for your second question YES you need to change the code and add the

String dbname="database_alon"

instead of

String dbName = "jdbctutorial";

I dont think you dont have a username in you database because it picks the root username by default. Try root for username and tell me what it gives you but make sure to update your classpath as stated above. Make also sure you are using the default port 3306 for connecting to the database.


P.S. What program you use for managing you databases? I can provide more help knowing that.

P.S.S You could use an IDE such as Eclipse or Netbeans and that could make the deployment much more easier. You will not need to touch the classpaths and other variables because the IDE will take care by just selecting the jars you want to use for your project.

P.S.S If you dont know how to add a jar in your classpath please provide your operating system in order to provide help for that.

Upvotes: 2

Related Questions