Вы находитесь на странице: 1из 5

package com.accenture1.

mypack;
import java.sql.*;
public class MySQL implements Database
{

public static void main(String[] args) {
new MySQL();
}
public MySQL()
{
//AccountHolder obj=obj=new AccountHolder(101,"Mahesh",5000);
//insertRow(obj);
//deleteRow(109);
//updateRow(obj);
/*obj=selectRow(109);
if(obj!=null)
{
System.out.println(obj);
}
else
{
System.out.println("Account holder is null");
}*/
}
public boolean insertRow(AccountHolder obj,Bank bobj)
{
Connection conn;
Statement stmt;
String str;
try
{
System.out.println("Inside insert method");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver has been loaded");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/te
st","root","root");
System.out.println("Connection has been established");

stmt=conn.createStatement();
System.out.println("Statement object created");
if(bobj instanceof Sbi)
{
str="Insert into sbi values(" +obj.getAccno()+ ','+"'" +obj.getN
ame()+ "'"+','+obj.getBalance()+ ");";
}
else
{
str="Insert into cbi values(" +obj.getAccno()+ ','+"'" +
obj.getName()+ "'"+','+obj.getBalance()+ ");";
}
System.out.println("str= " +str);
int nrows=stmt.executeUpdate(str);
if(nrows==1)
{
System.out.println("1 row inserted");
}
}
catch(ClassNotFoundException e1)
{
System.out.println("Driver not loaded");
}
catch(SQLException e2)
{
System.out.println("SQL ERROR !");
}
return true;
}
public boolean updateRow(AccountHolder obj,Bank bobj)
{
Connection conn;
Statement stmt;
String str;
try
{
System.out.println("Inside update method");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver has been loaded");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/te
st","root","root");
System.out.println("Connection has been established");

stmt=conn.createStatement();
System.out.println("Statement object created");
if(bobj instanceof Sbi)
{
str="update sbi set balance="+obj.getBalance()+ " where accno ="
+obj.getAccno()+';' ;
}
else
{
str="update cbi set balance="+obj.getBalance()+ " where
accno ="+obj.getAccno()+';' ;
}
System.out.println("str= " +str);
int nrows=stmt.executeUpdate(str);
if(nrows==1)
{
System.out.println("1 row updated");
}
else
{
System.out.println("Update failed...");
}
}
catch(ClassNotFoundException e1)
{
System.out.println("Driver not loaded");
}
catch(SQLException e2)
{
System.out.println("SQL ERROR !");
}
return true;
//System.out.println("1 row updated in MySQL");
//return true;
}
public boolean deleteRow(int accno,Bank bobj)
{
Connection conn;
Statement stmt;
String str;
try
{
System.out.println("Inside delete method");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver has been loaded");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/te
st","root","root");
System.out.println("Connection has been established");

stmt=conn.createStatement();
System.out.println("Statement object created");
if(bobj instanceof Sbi)
{
str="Delete from sbi where Accno="+accno;
}
else
{
str="Delete from cbi where Accno="+accno;
}
System.out.println("str= " +str);
int nrows=stmt.executeUpdate(str);
if(nrows==1)
{
System.out.println("1 row deleted");
}

else
{
System.out.println("Delete failed....Row not found");
}
}
catch(ClassNotFoundException e1)
{
System.out.println("Driver not loaded");
}
catch(SQLException e2)
{
System.out.println("SQL ERROR !");
}
return true;
//System.out.println("1 row deleted in MySQL");
//return true;
}
public AccountHolder selectRow(int accno,Bank bobj)
{ AccountHolder obj=new AccountHolder();
Connection conn;
Statement stmt;
String str;
try
{
System.out.println("Inside SELECT method");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver has been loaded");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/te
st","root","root");
System.out.println("Connection has been established");

stmt=conn.createStatement();
System.out.println("Statement object created");
if(bobj instanceof Sbi)
{
str="select accno,hname,balance from sbi where accno= "+accno;
}
else
{
str="select accno,hname,balance from cbi where accno= "+
accno;
}
System.out.println("str= " +str);
ResultSet rs =stmt.executeQuery(str);
rs.next();
obj.setAccno(rs.getInt(1));
obj.setName(rs.getString(2));
obj.setBalance(rs.getInt(3));
System.out.println("Record has been read");
return obj;
}

catch(ClassNotFoundException e1)
{
System.out.println("Driver not loaded");
}
catch(SQLException e2)
{
System.out.println("SQL ERROR !");
}
//System.out.println("1 row selected in MySQL");
return obj;
}
}