Sat. Sep 26th, 2020

How to Connect to MySQL database in Java programming
In this instructional exercise, you will figure out how to associate with MySQL database utilizing JDBC Connection object. To interface with MySQL database from a Java program, you need …

Program to create connection with database in java programming language.

/*
 create database deep;  
use deep;  
create table emp(id int(10),name varchar(40),age int(3));  
*/


 import java.sql.*;  
class MysqlConn
{  
public static void main(String args[])
{  
try
{  
Class.forName("com.mysql.jdbc.Driver");
  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/deep","root","root"); 
 
//here deep 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);}  
}  
}

Leave a Reply

Your email address will not be published. Required fields are marked *