0% found this document useful (0 votes)
7 views

Adv Java Experiment 03

Uploaded by

m.sahil2604a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Adv Java Experiment 03

Uploaded by

m.sahil2604a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Poornima College of Engineering, Jaipur

Experiment – 03
Advance Java Lab (5CS4-24)
Class – B.Tech III Year, V Sem.

Objective:

3.1 - Write a Java program that makes a Connection with database using JDBC and prints
metadata of this connection.
Code:
import java.sql.*;
class MysqlCon{
public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","root","root");
//here db 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);}
}
}

Objective – 3.2 Include the database Connectivity in the program to insert, update, delete and display of
information.

package com.devdaily.sqlprocessortests;

import java.sql.*;
public class BasicJDBCDemo
Poornima College of Engineering, Jaipur

Connection conn;
public static void main(String[] args)
{
new BasicJDBCDemo();
}
public BasicJDBCDemo()
{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();

String url = "jdbc:mysql://localhost/coffeebreak";


conn = DriverManager.getConnection(url, "username", "password");

doTests();
conn.close();
}
catch (ClassNotFoundException ex) {

System.err.println(ex.getMessage());

} catch (IllegalAccessException ex) {

System.err.println(ex.getMessage());

} catch (InstantiationException ex) {

System.err.println(ex.getMessage());

} catch (SQLException ex){

System.err.println(ex.getMessage());

}
}
private void doTests()
Poornima College of Engineering, Jaipur

{
doSelectTest();
doInsertTest();

doSelectTest();

doUpdateTest();

doSelectTest();

doDeleteTest();

doSelectTest();

}
private void doSelectTest()
{
System.out.println("[OUTPUT FROM SELECT]");
String query = "SELECT COF_NAME, PRICE FROM COFFEES";
try
{
Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(query);

while (rs.next())
{

String s = rs.getString("COF_NAME");

float n = rs.getFloat("PRICE");

System.out.println(s + " " + n);


}
}
Poornima College of Engineering, Jaipur

catch (SQLException ex)


{
System.err.println(ex.getMessage());
}}
private void doInsertTest()
{
System.out.print("\n[Performing INSERT] ... "); try
{
Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('BREAKFAST BLEND', 200, 7.99, 0, 0)");
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
}
private void doUpdateTest()
{

System.out.print("\n[Performing UPDATE] ... "); try


{
Statement st = conn.createStatement();
st.executeUpdate("UPDATE COFFEES SET PRICE=4.99 WHERE
COF_NAME='BREAKFAST BLEND'");
}
catch (SQLException ex){
System.err.println(ex.getMessage());}
Poornima College of Engineering, Jaipur

}
private void doDeleteTest()
{

System.out.print("\n[Performing DELETE] ... "); try


{
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM COFFEES WHERE COF_NAME='BREAKFAST BLEND'");
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
}
Poornima College of Engineering, Jaipur

Department of Computer Engineering

You might also like