The following code illustrate how to connect mysql database using jdbc . It is very simple go through it and nothing fancy in it!!!
package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); } catch (SQLException e) { System.err.println("something went wrong in database connection"); e.printStackTrace(); } finally{ if(conn!=null){ conn.close(); } } } }
This article is well explained. it is useful.
ReplyDeleteOracle Database Connectivity in Java .