Question: I would like to know how to connect to database in which is hosted in Xampp MySQL.

This is what I have so far in my java code to connect but I'm unsure what I'm doing. 

```
public static void main(String[] args) {
    try {
        Connection con = DriverManager.getConnection( host, username, password );
        String host = "jdbc:derby://localhost:1527/Employees";
        String uName = "root";
        String uPass= "password";
    }
    catch ( SQLException err ) {
    System.out.println( err.getMessage( ) );
    }

}

```

1. What would the host URL be?
2. Do i need a JDBC Jar file to connect to the DB?

I have setup a database and table via phpMyAdmin already. Just don't know how to proceed.

I am using **Netbeans** to write my Java code to connect to a local database which was created via **Xampp PHPMyAdmin**.

In the end, I want to create a database connection in Java and call out tables within the IDE. Would appreciate some help.

Answer: 
Database Connectivity:

These are following step are required to connect java application with database.

• Import the sql package

```
Import.java.sql.*;

```

• Load driver 

```
Every database has diffirent driver,we are using sql database so driver for sql database is

```

Class.forName(“com.mysql.jdbc.Driver”);

• Make Url

```
String url=”jdbc:mysql://localhost/DataBaseName”;

```

• Make Connection

```
Connection con=DriverManager(url,”root”,””);

```

• Create Statement object

```
Statement st=con.CreateStatement();

After creating statement object,we can perform sql queries on database,

```

• Make query

```
String sql=”SELECT * FROM table_Name ”;

```

• Make ResultSet Object and Excute query

```
ResultSet rs=st.excuteUpdate(sql);

```

• Important step to close the connection

```
con.close();

```

**Note** if we do not have sql connecter in jdk first download connecter from this url
<https://dev.mysql.com/downloads/connector/j/5.1.html>
and copy and paste into,

Java/jre/lib/ext
Use the below code. This will work

```
import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/sonoo","root","root");  
//here is database name, root is username and password  
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from emp");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
con.close();  
}catch(Exception e){ System.out.println(e);}  
}  
}  

```