Steps To Connect A Java Application To Database
Steps To Connect A Java Application To Database
Steps To Connect A Java Application To Database
The following 5 steps are the basic steps involve in connecting a Java
application with Database using JDBC.
The forName() method of Java Class class returns the Class object associated
with the class or interface with the given name in the parameter as String.
Syntax:
getConnection(String url)
getConnection(String url, Properties prop)
getConnection(String url, String user, String password)
Create a Connection
Syntax
getConnection(String url)
getConnection(String url, String username, String password)
getConnection(String url, Properties info)
Example establish connection with Oracle Driver
1. Class.forName("com.mysql.jdbc.Driver");
2. url="jdbc:mysql://localhost:3306/spring";
3. con = DriverManager.getConnection(url);
Syntax
try {
stmt = conn.createStatement( );
...
catch (SQLException e) {
Statement s=con.createStatement();
Execute SQL Statement
Syntax
ResultSet
The object of ResultSet maintains a cursor pointing to a row of a table.
Initially, cursor points to before the first row.
After executing SQL statement you need to close the connection and release
the session. The close() method of Connection interface is used to close the
connection.
Syntax
con.close();