Package daomysql;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.ArrayList;
Import java.util.List;
Import entity. Master;
Import SQL. Masterdao;
Import connection. Basedao;
/*
*petdao implementation Class (Master)
*/
public class Petdaomysqlimp extends Basedao implements masterdao{
Inserting data into the master table
public int Insert (Master mt) {
String sql= "INSERT into Master (Id,name,password,money) VALUES (?,?,?,?)";
Object[] Param={mt.getid (), Mt.getname (), Mt.getpassword (), Mt.getmoney ()};
int result=this.exceuteupdate (Sql,param);
return result;
}
Delete table contents
public int del (Master mt) {
String sql= "Delete from master where id=?";
Object[] Param={mt.getid ()};
int result=this.exceuteupdate (SQL, param);
return result;
}
Update Master Table
public int update (Master mt) {
String sql= "Update master set password=123456,money=900 where id=?";
Object[] Param={mt.getid ()};
int result=this.exceuteupdate (SQL, param);
return result;
}
Querying the contents of a table by a specified property
Public list<master> findbytype (int masterid) {
Connection Conn=null;
PreparedStatement Pstmt=null;
ResultSet Rs=null;
List<master> list=new arraylist<master> (); Create a Collection
try {
Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/epet", "root", "root");
String sql= "Select Masterid,name,password,money" + "from Master" + "where masterid=?";
Pstmt=conn.preparestatement (SQL);
Pstmt.setint (1,masterid);
Rs=pstmt.executequery ();
SYSTEM.OUT.PRINTLN ("Host's Information list");
SYSTEM.OUT.PRINTLN ("serial number \ t name \ t password \ t ingot number \ t");
while (Rs.next ()) {
Master Ms=new Master ();
Ms.setmasterid (Rs.getint ("MasterID"));
Ms.setname (rs.getstring ("name"));
Ms.setpassword (rs.getstring ("password"));
Ms.setmoney (Rs.getint ("money"));
List.add (MS);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
for (Master master:list) {
System.out.println (Master.getmasterid () + "\ T" +master.getname () + "\ T" +master.getpassword () + "\ T" +master.getmoney () );
}
return list;
}
}
JDBC Database increment, delete, change, check method realization class