+ All Categories
Home > Documents > Java MySQL Connectivity - WordPress.com · Java MySQL Connectivity By Prof. B.A.Khivsara Note: The...

Java MySQL Connectivity - WordPress.com · Java MySQL Connectivity By Prof. B.A.Khivsara Note: The...

Date post: 21-May-2020
Category:
Upload: others
View: 5 times
Download: 1 times
Share this document with a friend
19
Java MySQL Connectivity By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.
Transcript

Java MySQL Connectivity

By Prof. B.A.Khivsara

Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.

Implement MYSQL/Oracle database connectivity with PHP/ python/Java. Implement Database navigation operations (add, delete, edit,) using ODBC/JDBC.

Outline

Software Required and Steps

Create connection

Insert Data

Update Data

Delete Data

Display/Select Data

Program

Software Required and Steps

• Eclipse

• JDK 1.6

• MySQL

• Java-MySQL Connector

In Eclipse perform following steps:

1. File - New – Java Project –Give Project Name – ok

2. In project Explorer window- right click on project name-new- class- give Class name- ok

3. In project Explorer window- right click on project name- Build path- Configure build path- Libraries- Add External Jar - Java-MySQL Connector

4. In MySQL first Create one Database with name db1 and one table with name stud(name,age)

Outline

Software Required and Steps

Create connection

Insert Data

Update Data

Delete Data

Display/Select Data

Program

Import packages, Load Driver, Create connection with MySQL Database

• Import packages

import java.sql.*;

• Load Driver

Class.forName("oracle.jdbc.driver.OracleDriver");

• Create connection

Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/DatabaseName","username","passwd ");

Outline

Software Required and Steps

Create connection

Insert Data

Update Data

Delete Data

Display/Select Data

Program

Create JDBC statement(s)

8

• Creates a Statement object for sending SQL statements to the database

• Statement stmt = con.createStatement() ;

Executing SQL Statements

9

•executeUpdate – This Method is used for

insert, update and delete queries

• Example • String sql = “INSERT INTO stud VALUES (‘Ankita’,21)";

• stmt.executeUpdate(sql);

Executing SQL Statements

10

•executeUpdate – This Method is used for

insert, update and delete queries

• Example • String sql = “update stud set name=‘sumit’ where

name=‘Ankita’;

• stmt.executeUpdate(sql);

Executing SQL Statements

11

•executeUpdate – This Method is used for

insert, update and delete queries

• Example • String sql = “delete from stud where name=‘sumit’ ";

• stmt.executeUpdate(sql);

Outline

Software Required and Steps

Create connection

Insert Data

Update Data

Delete Data

Display/Select Data

Program

Get ResultSet (SELECT Query)

13

String sql1 = "SELECT name, age FROM stud"; //Write a select query in any string variable

ResultSet rs = stmt.executeQuery(sql1); //executeQuery used to run the select query and store the result in ResultSet

while (rs.next()) //Iterate through ResultSet till data is found

{

String name1 = rs.getString(“name"); //Store name data into name1 variable

int age1 = rs.getInt(“age"); //Store age data into age1 variable

}

Close connection

14

• stmt.close();

• con.close();

Outline

Software Required and Steps

Create connection

Insert Data

Update Data

Delete Data

Display/Select Data

Program

Sample program

16

import java.sql.*;

public class MySQLProcedure {

public static void main(String[] args) {

try{

Class.forName("com.mysql.jdbc.Driver");

Connection c = DriverManager.getConnection("jdbc:mysql://

localhost: 3306/ db1","root", "");

Statement sm = c.createStatement();

Sample program(cont)

int ch;

do {

System.out.println("Enter Chice \n 1.Insert \n 2.Select \n 3.Update \n 4.Delete \n 5.Exit ");

ch=s.nextInt();

switch (ch) {

case 1: sql = “Insert into stud values (‘Ankita’ ,1)";

sm.executeUpdate(sql);

System.out.println(“Record is Inserted");

break;

Sample program(cont)

18

case 2: String sql = "SELECT name, age FROM stud";

ResultSet rs = sm.executeQuery(sql);

while(rs.next())

{

String name1 = rs.getString("name");

int rno1 = rs.getInt(“rno");

System.out.print("name " + name1);

System.out.println(", Age: " + rno1);

}

break;

Sample program(cont)

case 3: sql = “update stud set name=‘Mona’ where name=‘Ankita’ ";

sm.executeUpdate(sql);

System.out.println(“Record is updated");

break;

case 4: sql = “delete from stud where rno=1";

sm.executeUpdate(sql);

System.out.println(“Record is deleted");

break;

} }while(ch<5);

}

catch(Exception e){

e.printStackTrace();

} } }


Recommended