JAVA PROGRAMMING
1
,JDBC contd…
JDBC is an interface which allows Java code to execute
SQL statements inside relational databases
The JDBC API is a Java API that can access any kind of
tabular data, especially data stored in a Relational
Database.
JDBC helps you to write Java applications that manage
these three programming activities:
1. Connect to a data source, like a database
2. Send queries and update statements to the
database
3. Retrieve and process the results received from the
database in answer to your query
2
,JDBC contd…
//The following simple code fragment gives a simple example of
these three steps:
public void connectToAndQueryDatabase(String username, String
password) { Driver Manager
Connection con = DriverManager.getConnection( Object
"jdbc:myDriver:myDatabase", username, password); Statement
Object
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
ResultSet
while (rs.next()) { Object
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
}
3
, 4