Java Database connection with MySql

import java.sql.*;
import java.sql.DriverManager
import java.sql.SQLException
public class demo1{ 
    public static void main(String[] args) { 
            String dburl= "jdbc:mysql://localhost/hemraj?user=root&password=";      
        try { 
            Class.forName("com.mysql.jdbc.Driver").newInstance()
            Connection conn = DriverManager.getConnection(dburl);
            Statement stmt = null
            ResultSet rs = null
            stmt = conn.createStatement()
            rs = stmt.executeQuery("SELECT *FROM users")
            System.out.println("Connection is : \n"+conn);
            while(rs.next()){
                System.out.println("Name    : "+rs.getString(1));
                System.out.println("ID : "+rs.getString(2));
                System.out.println("Password : "+rs.getString(3));
            }
            stmt.executeUpdate("create database abc");
            conn.close();
         } catch (Exception ex){    System.out.println(" Error : \n"+ex);}
    } 
}

No comments:

Popular Posts