Click here to watch in Youtube : https://www.youtube.com/watch?v=O4SNaqaWjA0
Click the below Image to Enlarge
JDBCConnectionDemo Project Dir Structure
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.PreparedStatement; public class JDBCConnectionDemo { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/world"; // Database credentials static final String USERNAME = "root"; static final String PASSWORD = "root"; public static void main( String[] args ) { JDBCConnectionDemo jdbcConnectionDemo = new JDBCConnectionDemo(); jdbcConnectionDemo.getConnection(); } private void getConnection() { Connection connection = null; Statement stmt = null; try { /* * Register the JDBC driver in DriverManager */ Class.forName(JDBC_DRIVER); /* * Establish connection to the Database using DriverManager */ connection = DriverManager .getConnection(DB_URL, USERNAME, PASSWORD); stmt = connection.createStatement(); System.out.println("Statement Object : "+ stmt); DatabaseMetaData dbmd=connection.getMetaData(); System.out.println("DatabaseMetaData Object : "+ dbmd); String sql = "select ID,Name,CountryCode,District,Population from city"; PreparedStatement preparedStatement = connection.prepareStatement(sql); System.out.println("preparedStatement Object : "+ preparedStatement); connection.close(); } catch( SQLException se ) { /* * Handle errors for JDBC */ se.printStackTrace(); } catch( ClassNotFoundException e ) { /* * Handle errors for Class.forName */ e.printStackTrace(); } finally { /* * finally block used to close resources */ try { if( stmt != null ) { stmt.close(); } } catch( SQLException sqlException ) { sqlException.printStackTrace(); } try { if( connection != null ) { connection.close(); } } catch( SQLException sqlException ) { sqlException.printStackTrace(); } } } }
Statement Object : com.mysql.jdbc.StatementImpl@4099a992 DatabaseMetaData Object : com.mysql.jdbc.JDBC4DatabaseMetaData@284bd160 preparedStatement Object : com.mysql.jdbc.JDBC4PreparedStatement@3068f00: select ID,Name,CountryCode,District,Population from city
JDK version :1.7.0_51
Tomcat version : 7.0.50
To Download JDBCConnectionDemoApp Project Click the below link
https://sites.google.com/site/javaee4321/jdbc/JDBCConnectionDemoApp.zip?attredirects=0&d=1
See also:
Thankyou for the Great Post.
ReplyDeleteInformatica Cloud Training in Hyderabad
Informatica cloud training
Informatica online training
Informatica cloud online training
Informatica Training in Hyderabad
Informatica Online Training in Hyderabad
Informatica Training in Ameerpet
Informatica cloud Online Training in Hyderabad
It is very useful information for us.Thanks for sharing. Nice Blog!!
ReplyDeleteInformatica Cloud Training in Hyderabad
Informatica cloud training
Informatica online training
Informatica cloud online training
Informatica Training in Hyderabad
Informatica Online Training in Hyderabad
Informatica Training in Ameerpet
Informatica cloud Online Training in Hyderabad