Adv Java Exp 3
Adv Java Exp 3
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();
Page |27
s to
sf
import java.sql.:
public class BasicJDBCDemo
Connection conn,
public static void main(String[] args)
newBasicJDBCDemo():
publicBasicJDBCDemo()
try
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
doSelectTest();
dolnsertTest(); doSelectTest():
doUpdateTest(); doSelectTest();
doDelete Test(): doSelectTest();
try
Statement st
conn.createStatement): ResultSetrs
=st.executeQuery(query); while
(rs.next))
Page | 29
ARYA Institute of Engg. &Technology
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
System.err.println(ex.getMessage(0);
}
private void doDeleteTest()
Statement st = conn.createStatement():
st.executeUpdate("DELETE FROM COFFEES WHERE COF NAME-'BREAKFAST
BLEND'");
System.err.println(ex.getMessage();
Page |31
E to
eEf
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?
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.
Page |32
AEngnoerin
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