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);}  
}  
}    

DeepCrazyWorld

Hey, I'm Deepika a professional blogger, web designer, Android developer and software engineer. I like to gain every type of knowledge that's why i have done many courses in different fields like engineering and technology. I love thrills and travelling to new places and hills.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.