Sunday, February 25, 2018

Advanced Java Program : JDBC connecting to a MySQL Database

import java.sql.*;
import java.io.*;
class CarsDb
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cars","AKSingh","ashutosh");
            Statement stmt = con.createStatement();
            ResultSet rs= stmt.executeQuery("select * from car_models");
            while(rs.next())
            {
                System.out.println(rs.getInt(1)+" "+rs.getString(2)+"   "+rs.getInt(3)+"   "+rs.getString(4)+"  "+
                rs.getInt(5));
            }   
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
Thanks
Happy Programming!

No comments:

Post a Comment

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...