import java.sql.*;
class Ass3{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
System.out.println("Connection Success");
PreparedStatement ps = conn.prepareStatement("CREATE TABLE COMP (NAME VARCHAR(20),QTY INT(5),PRICE DOUBLE(10,4))");//CREATE TABLE table_name (column_name column_type);
ps.executeUpdate();
System.out.println("Tbale created and data is inserting ...");
PreparedStatement ps1 = conn.prepareStatement("INSERT INTO COMP VALUES(?,?,?)");//ps1
//Set the values that will replace quesetion marks
String name="DELL";
int qty =1;
double price= 45000.45;
// now replace ?'s of PS1
ps1.setString(1,name);
ps1.setInt(2,qty);
ps1.setDouble(3,price);
ps1.executeUpdate();
System.out.println("values are inserted ....");
PreparedStatement ps2 = conn.prepareStatement("CREATE INDEX COMP1 ON COMP('NAME')");//ps1
ps2.executeUpdate();
System.out.println("INDEX ....");
PreparedStatement ps3 = conn.prepareStatement("SHOW INDEX FROM COMP");//ps1
ResultSet rs= ps3.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("Column_name"));
}
PreparedStatement ps4 = conn.prepareStatement("CREATE VIEW view AS SELECT PRICE,NAME FROM COMP");//ps1
ps4.executeUpdate();
PreparedStatement ps5 = conn.prepareStatement("SELECT * FROM view");//ps1
ResultSet rs2= ps5.executeQuery();
while(rs2.next())
{
System.out.print(rs2.getDouble("PRICE"));
System.out.println(rs2.getString("NAME"));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
class Ass3{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
System.out.println("Connection Success");
PreparedStatement ps = conn.prepareStatement("CREATE TABLE COMP (NAME VARCHAR(20),QTY INT(5),PRICE DOUBLE(10,4))");//CREATE TABLE table_name (column_name column_type);
ps.executeUpdate();
System.out.println("Tbale created and data is inserting ...");
PreparedStatement ps1 = conn.prepareStatement("INSERT INTO COMP VALUES(?,?,?)");//ps1
//Set the values that will replace quesetion marks
String name="DELL";
int qty =1;
double price= 45000.45;
// now replace ?'s of PS1
ps1.setString(1,name);
ps1.setInt(2,qty);
ps1.setDouble(3,price);
ps1.executeUpdate();
System.out.println("values are inserted ....");
PreparedStatement ps2 = conn.prepareStatement("CREATE INDEX COMP1 ON COMP('NAME')");//ps1
ps2.executeUpdate();
System.out.println("INDEX ....");
PreparedStatement ps3 = conn.prepareStatement("SHOW INDEX FROM COMP");//ps1
ResultSet rs= ps3.executeQuery();
while(rs.next())
{
System.out.println(rs.getString("Column_name"));
}
PreparedStatement ps4 = conn.prepareStatement("CREATE VIEW view AS SELECT PRICE,NAME FROM COMP");//ps1
ps4.executeUpdate();
PreparedStatement ps5 = conn.prepareStatement("SELECT * FROM view");//ps1
ResultSet rs2= ps5.executeQuery();
while(rs2.next())
{
System.out.print(rs2.getDouble("PRICE"));
System.out.println(rs2.getString("NAME"));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment