MYSQL and DAO knowledge summary

Java steps to connect to the database

1. Load the driver

Class.forName(数据库驱动名);

Database driver name in java: "com.mysql.jdbc.Driver"
Can be omitted in JavaSE project, JavaWeb project must write this step

2. Establish a database connection

Use the getConnection() static method of the DriverManager class to obtain the connection object of the database. The syntax is as follows:

Conncetion conn = DriverManager.getConnection(String url,String username,String password);

url --> the string of the database connection, the connection format of the string: main protocol: sub-protocol://ip address: port number/database name//
username --> database account
password --> database password

  • mysql connection address: "jdbc:mysql://localhost:3306/company?useUnicode=true&charcterEncoding=uft-8","root","123"
  • oracle connection address: jdbc:oracle:thin:@localhost:1521:ORCL

3. Create the Statement object

To operate on the database, you must execute SQL statements. In Java, if you want to transmit SQL statements to the database, you must use the Statement object to encapsulate them before sending them to the database. But Statement is not called directly, but through the methods provided by the Connection object to create various Statement objects. The syntax is as follows:

  1. Execute static SQL statements, usually through Statement instances;
  2. Execute dynamic SQL statements, usually through PreparedStatement instances;
  3. Execute database stored procedures, usually through CallableStatement instances.

Specific implementation:

Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql);
CallableStatement cstmt =   con.prepareCall("{CALL demoSp(? , ?)}")

4. Execute sql statement

After obtaining the Statement object, it is time to execute the SQL statement. At this time, you can call different methods of the object to operate the database. If a result set is generated, the result set must be encapsulated. In Java, the ResultSet class is used to encapsulate the result set, and a result set object will be returned.

Three methods of Statement interface

The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate and execute.

  1. ResultSet executeQuery(String sqlString): execute the SQL statement querying the database, and return a result set (ResultSet) object;
  2. int executeUpdate(String sqlString): used to execute INSERT, UPDATE or DELETE statements and SQL DDL statements, such as: CREATE TABLE and DROP TABLE, etc.;
  3. execute(sqlString): Used to execute statements that return multiple result sets, multiple update counts, or a combination of the two.
    Specific implementation code:
ResultSet rs = stmt.executeQuery("sql语句") ;   
int rows = stmt.executeUpdate("sql语句") ;   
boolean flag = stmt.execute(String sql) ;  

5. Use the result set

The result set is generally divided into two situations:

  1. The execution update returns the number of records affected by this operation;
int row = state.executeUpdate();
  1. The result of executing the query is a ResultSet object.
while(rs.next()){
    
       
	String name1 = rs.getString("name") ;   
    String name2 = rs.getString(1) ;   
}  

The purpose of the above two lines of code execution is the same, to obtain the data of the name character in the database, but the speed of obtaining name2 is faster than that of name1. (The column is from left to right, starting from 1)

6. Close the database

  1. Close the result set; rs.close();
  2. Close the Statement object; state.close();
  3. Close the conn connection. coon.close();

DAO factory

Insert picture description here

1. Design DAO interface

public interface IDao {
    
    
	public void createMethod();
}

Design abstract methods for adding, deleting, modifying, and checking inside

2. Design interface implementation class DAOJdbc

public class DaoJdbc implements IDao {
    
    
	@Override
	public void createMethod() {
    
    
		System.out.println("DaoJdbc::create()");
	}
}

3. Design a singleton factory

Features of singleton DAO factory:

  1. A singleton class can only have one instance;
  2. The singleton class must create a unique instance for itself;
  3. Singleton class must provide this instance to all other objects
public class DaoFactory {
    
    
	//1.私有化构造函数,不能让外界进行对象创建
	private DaoFactory(){
    
    }
	
	//2.这个类必须自动向整个系统提供这个实例对象
	public static DaoFactory getDaoFactory(){
    
    
		return daoFactory;
	}
	
	//3.单例类必须执行创建
	private static DaoFactory daoFactory;
	public IDao getDao(){
    
    
		//项目总根据需求对这里的内容进行修改,决定工厂提供什么样的产品
		return new DaoJdbc();
	}
}

4. Business layer


ublic class Service {
    
    
	public void create(){
    
    
		DaoFactory daoFactory = DaoFactory.getDaoFactory();
		IDao iDao = daoFactory.getDao();
		iDao.create();
	}

5. Test function

public class Demo1 {
    
    
	public static void main(String[] args) {
    
    
		Service ser = new Service();
		ser.createMethod();
	}
}

Guess you like

Origin blog.csdn.net/weixin_46687295/article/details/106725610