Sunday, March 4, 2018

Advanced Java programming: A JDBC application


package jdbcapp1;

//import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Ashutosh K Singh
 */
public class JdbcApp1 {

        public static void main(String[] args)   //throws ClassNotFoundException
    {
        // TODO code application logic here
        try
        {   
        System.out.println("Ashutosh");
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/mybooks","AKSingh", "ashutosh");
        Statement statement=connection.createStatement();
        String q1="create table books(id int auto_increment, subject varchar(30), pages int, price varchar(30),primary key(id))";
        int a= statement.executeUpdate(q1);
        System.out.println("table is created sucessfuly ="+a);
        String q2="insert into books values(1,'C by Ashutosh',200,'Rs 230/-')";
        String q3="insert into books values(2,'C++ by Ashutosh',400,'Rs 530/-')";
        String q4="insert into books values(3,'Java by Ashutosh',350,'Rs 430/-')";
        String q5="insert into books values(4,'Android by Ashutosh',700,'Rs 2230/-')";
        statement.executeUpdate(q2);
        statement.executeUpdate(q3);
        statement.executeUpdate(q4);
        statement.executeUpdate(q5);
        System.out.println("values included sucessfuly");
        String q6="select * from books";
        ResultSet set=statement.executeQuery(q6);
        while(set.next())
        {
            System.out.println(set.getInt(1)+" "+set.getString(2)+" "+set.getInt(3)+" "+set.getString(4));
        }
        connection.close();
        }
   
        catch(ClassNotFoundException | SQLException 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