Detailed explanation of JDBC and database connection code

Detailed explanation of JDBC and database connection code

Java implements interaction with the database:

 

package com.tangjiang.crm;

import java.io.Serializable;

/**
 * @author TangJiang on October 30, 2017 at 8:12:49 PM
 *
 */
public class Customer implements Serializable {

	/** Customer Number*/
	private long cId;
	/** Customer Name*/
	private String cName;
	/** Customer phone number */
	private String cPhone;
	/** customer address*/
	private String cAddress;

	public Customer() {
		super();
	}

	public Customer(long cId, String cName, String cPhone, String cAddress) {
		super();
		this.cId = cId;
		this.cName = cName;
		this.cPhone = cPhone;
		this.cAddress = cAddress;
	}

	public long getcId() {
		return cId;
	}

	public void setcId(long cId) {
		this.cId = cId;
	}

	public String getcName() {
		return cName;
	}

	public void setcName(String cName) {
		this.cName = cName;
	}

	public String getcPhone() {
		return cPhone;
	}

	public void setcPhone(String cPhone) {
		this.cPhone = cPhone;
	}

	public String getcAddress() {
		return cAddress;
	}

	public void setcAddress(String cAddress) {
		this.cAddress = cAddress;
	}

}

 

package com.tangjiang.crm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Database operation class
 * @author TangJiang on October 30, 2017 at 8:35:51 PM
 *
 */
public class CustomerDao {

	/**
	 * Save the object to the database
	 *
	 * @param object to save to database
	 * @return
	 */
	public void save(Customer c) {
		String sql = "insert into customers values(customer_sq.nextval,?,?,?)";
		// get database connection
		Connection conn = DbUtil.getCon();
		// precompile sql statement
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			// set the value of the parameter
			ps.setString(1, c.getcName());
			ps.setString(2, c.getcPhone());
			ps.setString(3, c.getcAddress());
			// execute sql
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}

	/**
	 * Delete customer information based on id
	 *
	 * @param
	 * @return
	 */
	public void remove(long id) {
		String sql = "delete from customers where id=?";
		// get database connection
		Connection conn = DbUtil.getCon();
		// get precompiled sql statement
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			// set parameter value
			ps.setLong(1, id);
			// execute sql statement
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}

	}

	/**
	 * Update the database according to the object
	 *
	 * @param object
	 * @return
	 */
	public void update(Customer c) {
		String sql = "update customers set name=?,phone=?,address=? where id=?";
		// get database connection
		Connection conn = DbUtil.getCon();
		// precompile sql statement
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			// set parameter value
			ps.setString(1, c.getcName());
			ps.setString(2, c.getcPhone());
			ps.setString(3, c.getcAddress());
			ps.setLong(4, c.getcId());
			// execute sql
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}

	/**
	 * Query all data
	 *
	 * @param
	 * @return returns a list of data
	 */
	public List<Customer> list() {
		// Set the collection to store the result set data
		LinkedList<Customer> list = new LinkedList<Customer>();
		String sql = "select *from customers";
		Connection conn = DbUtil.getCon();

		try {
			// precompile sql statement
			PreparedStatement ps = conn.prepareStatement(sql);
			// execute to get the result set
			ResultSet rs = ps.executeQuery();
			// Convert the result set to LinkedList
			while (rs.next()) {
				Customer c = new Customer();
				c.setcId(rs.getInt("id"));
				c.setcName(rs.getString("name"));
				c.setcPhone(rs.getString("phone"));
				c.setcAddress(rs.getString("address"));
				list.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		return list;
	}

}

 

package com.tangjiang.crm;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Database tool class, using lazy singleton pattern,
 * @author TangJiang
 * October 30, 2017 at 8:18:54 pm
 *
 */
public class DbUtil {
	
    //Define a Properties object
	static Properties p=new Properties();
	
	//Define the data attribute that stores the obtained data
	private static String driver;
	private static String url;
	private static String user;
	private static String pwd;
	
	//Define static block to implement configuration file loading
	static{
		try {
			p.load(DbUtil.class.getClassLoader()
					.getResourceAsStream("com/tangjiang/crm/jdbc.properties"));
			
			//Get the value corresponding to each key through the configuration file object
			driver=p.getProperty("driver");
			url=p.getProperty("url");
			user=p.getProperty("user");
			pwd=p.getProperty("pwd");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}
	
	
	/**
	 * Database connection establishment method (only one address)
	 * @return
	 */
	public static Connection getCon(){
		/**Database connection object, default is null*/
		Connection con=null;
		
		try {
			// Load the database driver package
			Class.forName(driver);
			//Get the database connection object
			con=DriverManager.getConnection(
					url,
					user,
					pwd);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		System.out.println("Database connection is successful");
		return con;
	}
	
	
	/**
	 * Define a close database link object to release resources
	 */
	public static void closeCon(Connection con,ResultSet rs,Statement stm,PreparedStatement ps){
		try {
		//close the object
		if(rs!=null){
			rs.close();
		}
		if(stm!=null){
			stm.close();
		}
		if(ps!=null){
			ps.close();
		}
		if(con!=null){
			con.close();
		}
		}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
		}
}

 

package com.tangjiang.crm;

import java.util.List;

public class Test {
	public static void main(String[] args) {
		CustomerDao cd = new CustomerDao();
		List<Customer>list=cd.list();
		for(Customer c:list){
			System.out.println(c.getcName());
		}
	}
}

 The configuration file code is not pasted! ! !

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611377&siteId=291194637