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

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows