JDBC add delete change and look up unique full code parsing

  • 2020-05-26 08:33:20
  • OfStack

Part 1 code (entity class)


package com.wf.entity;
public class Hehe{
private int hehe_id;
private String hehe_name;
private String hehe_gender;
public int getHehe_id(){
return hehe_id;
}
public void setHehe_id(int heheId){
hehe_id=heheId;
}
public String getHehe_name() {
return hehe_name;
}
public void setHehe_name(String heheName) {
hehe_name = heheName;
}
public String getHehe_gender() {
return hehe_gender;
}
public void setHehe_gender(String heheGender) {
hehe_gender = heheGender;
}
}

Part 2: BaseDao (addition, deletion, alteration and verification 1)


package com.wf.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao{
protected static final String DRIVER="com.mysql.jdbc.Driver";
protected static final String URL="mysql://localhost:3306/mysql";
protected static final String USER="root";
protected static final String PASSWORD="******";
protected Connection connection=null;
protected PreparedStatement preparedStatement=null;
protected ResultSet resultSet =null;
protected void getconnection() throws ClassNotFoundException, SQLException{
  Class.forName(DRIVER);
  this.connection =DriverManager.getconnection (URL,USER,PASSWORD);
}
/**
*  General methods of addition, deletion and modification 
* @param sql SQL statements 
* @param params  Parameters of the array 
* @return  Number of affected rows 
*/
protected int executeUpdate(String sql ,String[]params){
int result=-1;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
} 
}
result= this.preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.close();
}
return result;
}
/**
*  Query the result set method 
* @param sql
* @param params
*/
protected void executeQuery(String sql,String[]params){
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
} 
}
this.resultSet=this.preparedStatement.executeQuery();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
*  The query only 1 The results of the 
* @return Object
*/
protected Object executeQueryUnique(String sql,String[]params){
Object object=null;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
} 
} 
this.resultSet=this.preparedStatement.executeQuery();
if(this.resultSet.next())
object=this.resultSet.getObject(1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return object;
}
protected void close(){
try {
if(null!=this.resultSet)
this.resultSet.close();
if(null!=this.preparedStatement)
this.preparedStatement.close();
if(null!=this.connection)
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

Part 3 HeheDao


package com.wf.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wf.entity.Hehe;
public class HeheDao extends BaseDao{
public boolean insert(Hehe hehe){
String sql="insert into hehe(hehe_name,hehe_gender) values(?,?)" ;
String []params=new String[]{hehe.getHehe_name(),hehe.getHehe_gender()};
return this.executeUpdate(sql, params)>0? true:false;
}

Part 4 tests Test_BaseDao_Insert


package com.wf.test;
import com.wf.dao.HeheDao;
import com.wf.entity.Hehe;
public class Test_BaseDao_Insert {
public static void main(String[] args) {
Hehe hehe=new Hehe();
hehe.setHehe_name(" a ");
hehe.setHehe_gender("b");
HeheDao _hd=new HeheDao();
if(_hd.insert(hehe))
System.out.println(" successful !");
else
System.out.println(" failure !");
}
}


Related articles: