Student.java
package student;
import java.io.*;
import java.sql.*;
public class Student {
private Connection Database;
private Statement DataRequest;
private ResultSet Results2;
public Student()
{ String url = "jdbc:mysql://localhost:3306/studentdb";
String userID = "root";
String password = "";
try {
Class.forName( "com.mysql.jdbc.Driver");
Database = DriverManager.getConnection(url,userID,password);
}
catch (ClassNotFoundException error){
System.err.println("Unable to load the MySql Driver" + error);
System.exit(1); }
catch (SQLException error){
System.err.println("Cannot connect to the database." + error);
System.exit(2);}
try{
while(true)
{ System.out.println("1. Queries like:: create table/view,alter,drop,update and
delete");
System.out.println("2. Quries like:: Insert");
System.out.println("3. Queries like:: Selection/Calculation/Group
By/OrderBy/Join/Conditional Testing Query");
System.out.println("4. Exit");
InputStreamReader isr = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader(isr) ;
System.out.println("Select your choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{ case 1:
try {
System.out.println("Enter your query");
String q1 = br.readLine();
DataRequest = Database.createStatement();
DataRequest.execute(q1);
System.out.println("Query Executed successfully");
DataRequest.close();
}
catch(SQLException sqle){System.err.println(sqle);}
break;
case 2:
try {
PreparedStatement pst ;
System.out.println("Enter your query");
String q2 = br.readLine();
pst=Database.prepareStatement(q2);
pst.execute();
System.out.println("Record inserted Successfully.....");
pst.close();
}
catch(SQLException e){System.out.println(e);}
break;
case 3:
try
{
System.out.println("\nEnter the query to be executed\n");
String str=br.readLine();
DataRequest=Database.createStatement();
Results2=DataRequest.executeQuery(str);
DisplayResults(Results2);
DataRequest.close();
}
catch(Exception e){System.err.println(e);}
break;
case 4: System.exit(0);
}
}
}
catch(IOException ioe1){System.err.println(ioe1);}
}
private void DisplayResults (ResultSet Results2) throws SQLException
{
ResultSetMetaData rmd=Results2.getMetaData();
int col=rmd.getColumnCount();
int count=1;
boolean b=Results2.next();
if(!b)
{
System.out.println("No Data Found");
}
else
{
do
{ System.out.print("RECORD " +(count++)+" => ");
for(int i=0;i<col;i++)
System.out.print(Results2.getString(i+1)+"\t");
System.out.println();
}while(Results2.next());
}
}
public static void main(String[] args) {
// TODO code application logic here
final Student sdb = new Student();
}
}
package student;
import java.io.*;
import java.sql.*;
public class Student {
private Connection Database;
private Statement DataRequest;
private ResultSet Results2;
public Student()
{ String url = "jdbc:mysql://localhost:3306/studentdb";
String userID = "root";
String password = "";
try {
Class.forName( "com.mysql.jdbc.Driver");
Database = DriverManager.getConnection(url,userID,password);
}
catch (ClassNotFoundException error){
System.err.println("Unable to load the MySql Driver" + error);
System.exit(1); }
catch (SQLException error){
System.err.println("Cannot connect to the database." + error);
System.exit(2);}
try{
while(true)
{ System.out.println("1. Queries like:: create table/view,alter,drop,update and
delete");
System.out.println("2. Quries like:: Insert");
System.out.println("3. Queries like:: Selection/Calculation/Group
By/OrderBy/Join/Conditional Testing Query");
System.out.println("4. Exit");
InputStreamReader isr = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader(isr) ;
System.out.println("Select your choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{ case 1:
try {
System.out.println("Enter your query");
String q1 = br.readLine();
DataRequest = Database.createStatement();
DataRequest.execute(q1);
System.out.println("Query Executed successfully");
DataRequest.close();
}
catch(SQLException sqle){System.err.println(sqle);}
break;
case 2:
try {
PreparedStatement pst ;
System.out.println("Enter your query");
String q2 = br.readLine();
pst=Database.prepareStatement(q2);
pst.execute();
System.out.println("Record inserted Successfully.....");
pst.close();
}
catch(SQLException e){System.out.println(e);}
break;
case 3:
try
{
System.out.println("\nEnter the query to be executed\n");
String str=br.readLine();
DataRequest=Database.createStatement();
Results2=DataRequest.executeQuery(str);
DisplayResults(Results2);
DataRequest.close();
}
catch(Exception e){System.err.println(e);}
break;
case 4: System.exit(0);
}
}
}
catch(IOException ioe1){System.err.println(ioe1);}
}
private void DisplayResults (ResultSet Results2) throws SQLException
{
ResultSetMetaData rmd=Results2.getMetaData();
int col=rmd.getColumnCount();
int count=1;
boolean b=Results2.next();
if(!b)
{
System.out.println("No Data Found");
}
else
{
do
{ System.out.print("RECORD " +(count++)+" => ");
for(int i=0;i<col;i++)
System.out.print(Results2.getString(i+1)+"\t");
System.out.println();
}while(Results2.next());
}
}
public static void main(String[] args) {
// TODO code application logic here
final Student sdb = new Student();
}
}
Can u show me the output of this program please
ReplyDelete