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.
Example:
In this example, silan is the database name, root is the username and password is the passwordname.
MysqlConDemo.java 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.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc