KaJasB
KaJasB

Reputation: 2545

Java Connect Database on Site

Hi guy i want to connect to my database on my website using java. i wonder what should i put in URL:

cnt=DriverManager.getConnection(DB_URL, DB_UID,DB_PWD);

Can you guy give me an example how to connect Mysql Database on website using java?

i have try to use:

import java.sql.*;

public class CreateConnect {


public final static String DB_DRIVER="com.mysql.jdbc.Driver";
public final static String SEVER_HOST="mysite";
public final static String PORT_NUM="3306";

public final static String DB_NAME="studentdb";
public final static String DB_UID="root";
public final static String DB_PWD="root";

public final static String DB_URL="jdbc:mysql://"+SEVER_HOST+":"+PORT_NUM+"/"+DB_NAME;

private static Connection cnt;

public static Connection getConnection()
{

    try
    {

        cnt=DriverManager.getConnection(DB_URL, DB_UID,DB_PWD);
        System.out.println("Connection Successfully");
    }
    catch(SQLException e)
    {
        System.out.println("Connection ERROR");
        e.printStackTrace();
    }

    return cnt;
}

Upvotes: 2

Views: 126

Answers (2)

Helios
Helios

Reputation: 851

I guess I understood your problem but if you could tell me the error that would have been more helpful to debug.

Add this line before calling getConnection method.

Class.forName ("com.mysql.jdbc.Driver").newInstance ();

I hope it should work now.

Upvotes: 1

Arun
Arun

Reputation: 105

The below reference can help you connect to the database which is hosted.you have to provide the IPaddress of the site and port number 3306 to connect to the database.

JDBC: connect to remote mySQL database?

Upvotes: 0

Related Questions