Sunday, February 25, 2018

Advanced Java program : JDBC interacting with a Database

import java.sql.*;
import java.io.*;

class Cars_db
{   
   
    public static void main(String[] args)
    {
        FileOutputStream f = new FileOutputStream("Cars database JDBCquery.txt");
        System.setOut(new PrintStream(f));
        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));
            }   
            con.close();
        }
       
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
Thanks
Happy Programming!

No comments:

Post a Comment

Sacred Thought

28 April 2024 Today I am going to explain verse 31 - 38 chapter two for you all. There is no opportunity better than a righteous war (सत्य औ...