Return to Snippet

Revision: 72288
at June 27, 2017 18:42 by syedhussim


Initial Code
import  java.sql.*;
import  java.sql.Connection;
import  java.sql.DriverManager;
                
class  Example{
        public  static  void  main(String  args[]){
                String  driver  =  "com.mysql.jdbc.Driver";
                String  connection  =  "jdbc:mysql://localhost:3306/database_name";
                String  user  =  "";
                String  password  =  "";
                try{
                        Class.forName(driver);
                        Connection  con  =  DriverManager.getConnection(connection,  user,  password);
                        if  (!con.isClosed()){
                                Statement  stmt  =  con.createStatement();    
                                
                                ResultSet  rs  =  stmt.executeQuery("SELECT * FROM employees");      
                        
                                while(rs.next()){
                                        System.out.println(rs.getString("First_Name"));    
                                }
                        }
                }catch(Exception  e){
                        System.out.println(e.getMessage());
                }
        }
}

Initial URL
http://www.snippetcentral.net/java/connect-to-a-mysql-database.html

Initial Description
This code snippet shows how to connect to a MySQL database using Connector/J.

Initial Title
Connect To A MySQL Database

Initial Tags
mysql

Initial Language
Java