- Load the JDBC Driver:
- Define the connection URL In JDBC, a connection URL specifies the server host, port, and database name with which to establish a connection.
- protocol:
jdbc - database-host :
mysql://localhost - port :
3306 - database-name :
test. - Establish the connection to Database The
- Create a Statement object . Creating a Statement object enables you to send queries and commands to the database.
- Execute a query or update . Given a Statement object, The
- Process the results. When a database query is executed, a ResultSet is returned. The ResultSet represents a set of rows and columns that you can process by calls to next and various getXxx methods.
- Close the connection. When you are finished performing queries and processing results, you should close the connection, releasing resources to the database.
Class.forName() ,create's a driver instance and register it with the JDBC driver manager.
Syntax:
Examples:
The above examples creates MySQL and OracleDriverdriver instance and register with JDBC.
Syntax: Example
MySQL : jdbc:mysql://localhost:3306/test ORACLE : jdbc:oracle:thin:@localhost:1521:xeIn the above example
getConnection() method of DriverManager class is used to establish connection with the database.With the connection URL, username,and password, a network connection to the database can be established.
Once the connection is established, database queries can be performed until the connection is closed.
Syntax: Example:
The
createStatement() method of Connection interface is used to create statement.Syntax: Example:
execute(),executeQuery(),executeUpdate(), or executeBatch() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table.
Syntax: Example: The above example describes, how to create and insert records into student table in database.
Syntax: Example: The above code snippet is used to display list of names in Student table.
Syntax: Example
0 comments :
Post a Comment