Online-Academy

Look, Read, Understand, Apply

Menu

Connecting with MySQL Database

Connecting Java Program with MySQL database

To connect Java Program with Database System, we have to use API which will facilitate to make connection between Java Program and database. In this page, information about connecting Java Program with MySQL database is provided. The class required to make connection with Database system are:

  • Connection: The object of Connection class is created with the help of DriverManager.getConnection(). The getConnection() methods takes argument which specifies the url of database, username, and password.
  • Statement: The object of Statement class is created using object of Connection class. Object of Statement class is used to execute SQL statements. Method executeQuery is used to execute SELECT statement and executeUpdate is used to execute INSERT, DELETE, UPDATE statements.
  • ResultSet: Object of ResultSet class is used stored records returned as the result of execution of SELECT statement. Method next() is used to traverse throw the records stored in the ResultSet object. Method getDATATYPE(columnIndex) is used to accessed data of specified column. Loop statement is used to traverse the ResultSet object. When the next() method reaches each of the records it returns false and loop will terminate.
  • If we are using INERST, UPDATE or DELETE statement we don't need object of ResultSet class as these statement don't return set of records.
  • We have to import: java.sql. package
  • SQLException has to be catched.

import java.sql.*;
public class test_connection {
	public static void main(String[] ar){
		try{  
			   //String url = "jdbc:mysql://localhost:3306/test";
			   //String un = "root";
			   //String pw = "";
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=");
			   //Connection c = DriverManager.getConnection(url,un,pw);
			System.out.println("Connected!");
			   Statement st=(Statement) c.createStatement();  
			   
			   ResultSet rs=st.executeQuery("select pname,qty from product");  
			    
			   while(rs.next()){  
			    System.out.println(rs.getString(2)+" "+rs.getString(3));  
			   } 
			  
			}  
			catch(SQLException ee){System.out.println(ee.getMessage());}
	}
}

Inserting record to database

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.*;
import java.awt.event.*;

class Test extends JFrame implements ActionListener{
	JTextField jt1,jt2,jt3;
	JButton btn;
	public Test() {
		setLayout(new FlowLayout());
		setSize(200,300);
		setTitle("In Eclipse");
		setVisible(true);
		jt1 = new JTextField(10);
		jt2 = new JTextField(10);
		jt3 = new JTextField(10);
		add(jt1);add(jt2);add(jt3);
		btn = new JButton("Insert");
		add(btn);
		btn.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent aeeeee) {
		try{  
			   String url = "jdbc:mysql://localhost:3306/test";
			   String un = "root";
			   String pw = "";
			   Connection c = DriverManager.getConnection(url,un,pw);
				   System.out.println("Connected!");
			
			   Statement st=(Statement) c.createStatement();  
			    
			    int id = Integer.parseInt(jt1.getText());
			    String pname = jt2.getText();
			    int qty = Integer.parseInt(jt3.getText());
			    
	st.executeUpdate("insert into product values ("+id+",'"+pname+"',"+qty+")");
			}  
			catch(Exception ee){System.out.println(ee.getMessage());}	
	}
	public static void main(String[] ar){
		new Test();
	}
}