Like Connectivity with Oracle, we can connect java application with the mysql database. The following 5 steps are required to perform database connectivity.
Let's first create a table in the mysql database, but before creating table, we need to create database first.
In this example, silan is the database name, root is the username and password is the password name.
import java.sql.*;
class MysqlConDemo{
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/silan","root","password");
//here silan is database name, root is username and password is password name
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from customer");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}
}
The above example will retrieve all the records from customer table.