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

Adv Java Exp 3

Uploaded by

Rishabh Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Adv Java Exp 3

Uploaded by

Rishabh Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Engineerino ARYA Institute of Engg. &Technology Approved by AICTE.

New Delhi)
(Affiliated to University of Rajasthan/RTU
028 Website : www.aryainstitutejpr.com
KUKAS JAUR
S.P.-40, Kukas Industrial Area (RICO) Jaipur-302 FAX: 01426-510040
A -E-T7 " Ph.:0141- 5148801, 5148802, 5148803

PROGRAM- 3

Write a Java program that makes a Connection with database usingJDBCand prints \
metadata of this connection

importjava.sql.";
class MysqlCon
public static void main(String args[)D{
try!
Class.forName("com.mysql.jdbe. Driver")

Connection con=DriverManager.getConnection(
"jdbc:mysql://Mocalhost:3306/sonoo" "root","root");
/here sonoo is database name, root is username and
password Statement stmt=con.createStatement():

ResultSetrs=stmt.executeQuery("select * from
emp"); while(rs.next())
System.out.println(rs. getInt(1)+" "+rs.getString(2)+" "Ars.getString(3);
con.close();

catch(Exception e){ System.out. println(e); }

Include the database Connectivity in the program to insert, update, delete


and display ofinformation.
packagecom.devdaily.sqlprocessortests

Page |27
s to
sf

ARYA Institute of Engg.


(Aatod to Unversity RaiathanRTU
&Technology
Aporosef tby ARCTE, Ne Dedha)
WARAPL SP-40, Kukas industrial Area (RICO) Jaipur-302 028 Webte. wwsrysinsttelor com
SA1ET7 " Ph 0141- 5148801, 5148802. 5148803 .FAX: 01426-810040

import java.sql.:
public class BasicJDBCDemo

Connection conn,
public static void main(String[] args)

newBasicJDBCDemo():

publicBasicJDBCDemo()

try

Class.for Name( "com.mysql.jdbe.Driver").newlnstance();


String url ="jdbe:mysql:/Aocalhost/coffeebreak":;
conn = DriverManager.getConnection(url, "username",
"password"); doTests);
conn.close():

catch (Class NotFoundException


ex){System.er printin(ex getMessage);}catch (Ilegal AccessException
ex)(System.err.printlnex.getMessage();} catch (InstantiationException
cx) (System.ert.println(ex.getMessage)):} catch(sQLExceptioncx)
(System. er.println(ex.getMessage):}

Page | 28
osf
Eng
ste
ineering
ARYA Institute of Engg. &Technology
(Affiiated to University of Rajasthan/RTU Approved by AiCTE, New Delti)
KUKAS, JAPUR SP.-40, Kukas Industrial Area (RICO) Jaipur-302 028 Website: www.aryainstitutejpr.com
"Ph.:0141- 5148801, 5148802, 5148803 FAX : 01426-510040
SATET

private void doTests()

doSelectTest();
dolnsertTest(); doSelectTest():
doUpdateTest(); doSelectTest();
doDelete Test(): doSelectTest();

private void doSelectTest()

System.out.printin("[OUTPUT FROM SELECT]");


String query = "SELECT COF NAME, PRICE FROM COFFEES":

try

Statement st

conn.createStatement): ResultSetrs

=st.executeQuery(query); while

(rs.next))

String s = rs.getString("COF NAME");


float n=rs.getFloat("PRICE");
System.out.println(s +""+n);

catch (SQLException ex)

Page | 29
ARYA Institute of Engg. &Technology

Spsem e printini en geMesag

System out prnPefeeming INSERTT

eeoutel ipdate"NSERT INTO COPs

IPDATE
ARYA nstitute of Engg. &Technology
(ASiated to Unversity of Rajasthan/RTU " Aporoved by AlCTE, New Delh)
KUKAS. JAUR S.P-40, Kukas Industrial Area (RIICO) Jaipur-302 028 Website:www.aryainstitutejpr.com
A-1-ET "Ph.:0141- 5148801, 5148802, 5148803 FAX :01426-510040

st.cxecuteUpdate("UPDATE COFFEES SET PRICE=4.99


WHERE COF NAME="BREAKFAST BLEND");

catch (SQLException ex)

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

}
private void doDeleteTest()

System.out.print(" n[Performing DELETEJ.


"); try

Statement st = conn.createStatement():
st.executeUpdate("DELETE FROM COFFEES WHERE COF NAME-'BREAKFAST
BLEND'");

catch (SQLException ex)

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

Page |31
E to
eEf

ARYA Institute of Engg. &Technology


(ARliated to University of Rajasthan RTU Approved by A!CTE, New Dethi)
KUKAS, JAIPUR S.P-40, Kukas Industrial Area (RIICO) Jaipur-302 028 Website : www.aryainstitutejpr.corm
A-lET "Ph.:0141-5148801, 5148802. 5148803 FAX :01426-510040

Sr. No VIVA QUESTIONS


1. What is JDBC?

JDBC Is a Java API that is used to connect and execute the query to the database.
JDBC APIuses JDBCdrivers to connect to the database. JDBC API can be used to
access tabular data stored into any relational database.
2. What is JDBC Driver?

JDBC Driver is a software component that enables Java application to interact with
the database.
3. What is the return type of Class.forName) method?

The Class.forName) method returns the object of java.lang.Class object.


4. Howcan we set null value in JDBC Prepared Statement?

By using setNull) method of Prepared Statement interface, we can set the null value to an index.
The syntax of the method is given below.
void setNull(intparanmeterlndex, intsqlType) throws SQLException
5. What is the role of the JDBC DriverManager class?

The DriverManager class acts as an interface between user and drivers. It keeps track of the
drivers that are available and handles establishing aconnection between a database and the
appropriate driver. The DriverManager class maintains a list of Driver classes that have registered
themselves by calling the method
DriverManager.registerDriver().
6. What is the major difference between java.util.Date and java.sql.Date data
type?
The major difference between java.util.Date and java.sql.Date is that, java.sql.Date represents
date without time information whereas, java.util.Date represents both date and time information.

7. What does JDBC setMaxRows method do?


The setMaxRows(inti) method limits the number of rows the database can return by using the query.
This can also be done within the query as we can use the limitcause inMySQL.
What is connection pooling?

Page |32
AEngnoerin

ARYA Institute of Engg. &Technology


(Afiated to Universty of RajasthanRTU Approved by AlCTE, New Delhi)
KuKAS, JAIPUA S.P.-40, Kukas Industrial Area (RlICO) Jaipur-302 028 Website : www.aryainstitutejpr.com
FAX: 01426-510040
A:1:E7 " Ph.:0141-5148801, 5148802, 5148803

This is also one of the most popular question asked during JDBC Interviews. Connection pooling
1s the mechanism by which we reuse the recourse like connection objects which are nceded to
makeconnection with database In this mechanism client are not required every time make new
connection and then interact with database instead of that connection objects are stored in
connection pool and client will get it from there. so it's a best way to share a server resources
among the client and enhance the application performance.
9. Does the JDBC-ODBC Bridge support multiple concurrent open statements
per connection?

No,we can open only one statement object when using JDBC-ODBC Bridge.
10. What Does The Connection Object Represents?
The connection object represents communication context, i.e., all communication
With database is through connection object only.

Page |33

You might also like