This article connects to the SQL Server driver for the JDBC driver link that was used in the previous chapter
Package addressutil;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import javax.swing.text.DefaultEditorKit.InsertBreakAction;
Import Bookconnutil.dbutil;
public class Addressutil {
Private address address = new address ();
public static Boolean addressupdate (String name,string email,string phone,string company,string address) throws sqlexception{
String updatewithname = "Update address set email=?,phone=?,company=?,address=?where name=?";
Boolean flag =false;
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
try{
Conn=dbutil.getconnection (Dbutil.connection_sql);
Stmt=conn.preparestatement (Updatewithname);
int count=stmt.executeupdate ();
Stmt.setstring (1, email);
Stmt.setstring (2, phone);
Stmt.setstring (3, company);
Stmt.setstring (4, address);
Stmt.setstring (5, name);
if (count>0) {
Flag=true;
}else {
Flag=false;
}
}catch (Exception e) {
E.printstacktrace ();
//Do not have specific exception handling for it just a simple capture if you need to modify it yourself
}finally{
Conn.close ();
Stmt.close ();
}
return flag;
}
public static Boolean Addressselect (String name) throws sqlexception{
String selectaddress = "Select Email,phone,company,address,from address where name=?";
Boolean flag =false;
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
try{
Conn=dbutil.getconnection (Dbutil.connection_sql);
Stmt=conn.preparestatement (selectaddress);
int count=stmt.executeupdate ();
Stmt.setstring (1, name);
if (count>0) {
Flag=true;
}else {
Flag=false;
}
}catch (Exception e) {
E.printstacktrace ();
There is no specific exception handling, just a simple capture if you need to modify it yourself
}finally{
Conn.close ();
Stmt.close ();
}
return flag;
}
public static Boolean Addressdelete (String name) throws sqlexception{
String deleteaddress = "Delete * from address where name=?";
Boolean flag =false;
Connection Conn=null;
PreparedStatement Stmt=null;
ResultSet Rs=null;
try{
Conn=dbutil.getconnection (Dbutil.connection_sql);
Stmt=conn.preparestatement (deleteaddress);
int count=stmt.executeupdate ();
Stmt.setstring (1, name);
if (count>0) {
Flag=true;
}else {
Flag=false;
}
}catch (Exception e) {
E.printstacktrace ();
//Do not have specific exception handling for it just a simple capture if you need to modify it yourself
}finally{
Conn.close ();
Stmt.close ();
}
return flag;
}
public static Boolean Addressinsert (String name,string email,string phone,string company,string address) throws sqlexception{
Boolean flag =false;
Try
{
Connection Con=null;
Con=dbutil.getconnection (1);
String insert= "Insert into address values (?,?,?,?,?)";
PreparedStatement stmt = null;
stmt =con.preparestatement (Insert);
Stmt.setstring (1, name);
Stmt.setstring (2, email);
Stmt.setstring (3, phone);
Stmt.setstring (4, company);
Stmt.setstring (5, address);
System.out.println ("??????");
int count=stmt.executeupdate ();
System.out.println ("11111111111111111111111111111");
if (count>0)
{
Flag = true;
}else{
Flag=false;
}
} catch (Exception e) {
E.printstacktrace ();
System.out.println ("gone awry");
}
return flag;
}
}
Adding and pruning database operations