0

I have not used JDBC before. I want my program to store information from the user every time the user starts the program. Previous activity can be seen, so I have to use database, right?

So I thought of using JDBC but I have some problems.

import java.sql.* ; 
public class Database {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/";
    //  Database credentials
    static final String USER = "username";
    static final String PASS = "password";

    public Database(){
        Connection con = null;
        Statement stmt = null;
        try{
            Class.forName(JDBC_DRIVER);
        }catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }
        try{
            con = DriverManager.getConnection(DB_URL, USER, PASS);
        }catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }
    }
}

ERROR : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Caused by: java.net.ConnectException: Connection refused

  1. Where is this username and password used? In mysql installed on my laptop or does java have its own sql?
  2. Default username and password?
  3. If it is for mysql installed on my laptop, then what if I give my program to my friend to run and if he has a different username password for his mysql?
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tichu
  • 13
  • 1
  • 6

5 Answers5

0

Two Things:

  1. The default MySQL user is root and password will be what ever you set while installing and configuring. Mostly developers use root too.

  2. static final String DB_URL = "jdbc:mysql://localhost/";

You have not mentioned the name of your database to connect to and the port number in this string, which will be the real problem in your case I guess.

static final String DB_URL = "jdbc:mysql://localhost:3306/YOUR_DATABASE_NAME";

The default port number for MySQL is 3306

To create a database from the program you need to run the following:

Statement stmt = con.createStatement();
stmt.executeUpdate("create database DB_NAME");
gprathour
  • 14,813
  • 5
  • 66
  • 90
0

static final String DB_URL = "jdbc:mysql://localhost/DB_Name";

USER = "root"; PASSWORD= "";

The default credential are "root" and "" for everyone, you can export your DB from phpMyAdmin..you have the button called "Export" and you can pass that file to your friends

Renny
  • 114
  • 10
0
static final String DB_URL = "jdbc:mysql://localhost/";

Where is your database name and port in DB_URL ? Please change as below and let us know again the status ?

static final String DB_URL = "jdbc:mysql://localhost/3306/yourdb";

Here is the best example for Connect to MySQL with JDBC. http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

Ye Win
  • 2,020
  • 14
  • 21
0

@tichu your code will work just mention port number of your mysql and check your credentials are correct. I have run your program with my credentials and port number code run successfully with database creation. Just find the code below:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


/**
 *
 * @author manoj.sharma
 */
public class Test{

    /**
     * @param args the command line arguments
     */

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/";

   //  Database credentials
   static final String USER = "root";
   static final String PASS = "";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);

      //STEP 4: Execute a query
      System.out.println("Creating database...");
      stmt = conn.createStatement();

      String sql = "CREATE DATABASE STUDENTS";
      stmt.executeUpdate(sql);
      System.out.println("Database created successfully...");
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main

}
Manoj Sharma
  • 596
  • 1
  • 6
  • 23
  • How you login into mysql on your system? as i login into system using mysql workbench with credentials. or try to print your connection object like: System.out.println(conn); what output you will get just put here. – Manoj Sharma Dec 12 '14 at 05:59
  • it worked!!!! but i want to know what if i will give my program's executable file to someone else and he don't have mysql installed or have different credentials or xampp not started????? what is the solution??? – tichu Dec 12 '14 at 06:11
  • Well, for this your have to create an user interface to accept database information prior to establish database connection. When user fill the form store that information into variable and use accordingly. – Manoj Sharma Dec 12 '14 at 06:39
0

Your are getting such exception because your sql service is not running and you are trying to connect to sql. Username/Password should not be an issue.

Utkarsh Bhatt
  • 143
  • 5
  • 10