Example query of jdbc

PHOTO EMBED

Fri Nov 15 2024 14:06:25 GMT+0000 (Coordinated Universal Time)

Saved by @login123

 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
 import  java.sql.SQLException;
 import java.sql.DriverManager;
  
    public class JdbcExamplePrg1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
  
        String QUERY = "select * from book where id <46";
      
        Class.forName("com.mysql.cj.jdbc.Driver"); 
  
        try {
		
  Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root","ramesh");
        
                Statement statemnt1 = conn.createStatement();
              
          ResultSet rs1 = statemnt1.executeQuery(QUERY); 
                                      
                    while(rs1.next())
                    {
                        int id = rs1.getInt(1);
                        String title = rs1.getString(2);
                        String author = rs1.getString(3);
                        String publisher = rs1.getString(4);
                        int price = rs1.getInt(5);

                    System.out.println(id + ","+title+ ","+author+ ","+publisher +","+price );
                    }
                 
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
       }
    }
content_copyCOPY