1. To connect java into database, we need some driver to connect. I have used jdbc driver.
2. In java.sql package contain DriverManager class, it handles driver.
3. After this driver successfully connected to data base, it returns connection object.
DriverManager.getConnection(String databaseUrl, String username, String password);
datbaseUrl - Url which represents database connection
Example, jdbc:mysql://localhost:3306/Uma
Uma - Data base name
username - username of the mysql
password - Password of the mysql
4. Use this connection object, we can create a statement for executing mysql queries.
Statement st = Conn_object.createStatement() - create a statement with out any specification
[ We can create statement with some specification like resultset scrollable, etc ]
5. statement class contains methods to run queries. We can get the query result using ResultSet.
ResultSet rs = st.exceuteQuery(String query) - Simply returns the result, not update in datbase
st.updateQuery(String query) - Update the query result into database
Some methods in ResultSet :
We can get values from ResultSet using some methods in ResultSet.
1. To get the column value using getXXX()
Here XXX is the column data type like getInt(), getString()
2. To check wheather or not a column is null, using wasNull()
if (rs.wasNull()) {
// resultset contains null value
}
3. Use JDBC operation inside try block and catch the exception which is thrown. TO get more details about exception,
JDBC provides some methods, e.getMessage(), e.getErrorCode()
catch (Exception e) {
// print error message using e.getMessage()
// print error number using e.getErrorCode()
}
More Info :
http://www.kitebird.com/articles/jdbc.html
No comments:
Post a Comment