Data Persistence: saving data to a storage device to power-down for later use.
Main applications: data storage memory in the relational database, disk files, XML in.
In Java, database access technology:
1) JDBC direct access to the database (the cornerstone)
2) JDO technology
3) a third party O / R tool, such as Hibernate, ibatis like.
JDBC is independent of a particular database management system, generic SQL database access and operation announcement interface (a set of the API), defines a standard Java library for accessing the database .
JDBC provides a uniform way to access different databases.
JDBC goal is to make Java programmers can connect to any JDBC driver provides a JDBC database system.
JDBC Interface (API) consists of two levels:
Application-oriented API: Java API, abstract interface for use by applications developers to use (connect to the database, execute SQL statements, get results).
Database-oriented API: Java Driver API, for developers to build database-driven procedures.
JDBC Driver: each database vendor specifications produced by the JDBC JDBC implementation class library .
Pure Java drivers for local agreements : written entirely in Java, through the Socket connection established with the database, using a specific vendor's JDBC calls into the network protocol calls for a network directly connected.
The JDBC API is a set of interfaces that allow programs to be coupled to a database, execute the SQL statement, and returns the result obtained.
Java.sql.Driver interface is a database vendor must provide the interface, from the database connection .
In the program does not require direct access to the class that implements the Driver interface, but by the Driver Manager class ( java.sql.DriverManager ) to call the Driver implementation.
Join mysql driver
1) Create lib directory under the current project
2) Copy the mysql-connector-java-5.1.7-bin.jar lib directory to
3) Right build-path, add the classpath duildpath added to.
Call connect Driver class () method to establish a connection to the database:
JDBC URL used to identify a driver is registered, the driver manager by the URL selecting the correct driver , so as to establish a connection to the database.
Standard JDBC URL consists of three parts, separated by a colon between the parts.
- jdbc: <sub-protocol>: <child name>
- Protocol: JDBC URL is always jdbc protocol
- sub-protocols: for identifying a sub-protocol database driver
- Sub Title: A method for identifying a database, the name may vary depending on the sub-sub-protocols of different object names to sub-location database to provide sufficient information.
例:"jdbc:mysql://localhost/users"
public static void testDriver() throws SQLException {
//1.创建一个Driver实现类的对象
Driver driver=new com.mysql.jdbc.Driver();
//2.准备连接数据库的基本信息:url,user,password
String url="jdbc:mysql://localhost:3306/users";
Properties info=new Properties();
info.put("user", "root");
info.put("password","12345678");
//3.调用Driver接口的connect(url,info)获取数据库连接
Connection connection=driver.connect(url, info);
System.out.println(connection);
}
A general method of preparation of hair, without source code modification, can obtain a connection to any database.
Solution: to achieve full database-driven Driver class class name, url, user, password into a profile, and achieve decoupling specific database by modifying the configuration files.
public static Connection getConnection() throws Exception{
//1.准备连接数据库的4个字符串
//驱动的全类名
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;
//加载配置文件
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties=new Properties();
properties.load(loader.getResourceAsStream("jdbc.properties"));
//读取类路径下的jdbc.properties文件中连接数据库的4个字符串
driverClass=properties.getProperty("driver");//读取接口类型
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");
//2.创建一个Driver实现类的对象
Driver driver=(Driver)Class.forName(driverClass).newInstance();//反射
properties.put("user", user);
properties.put("password",password);
//3.调用Driver接口的connect(url,info)获取数据库连接
Connection connection=driver.connect(jdbcUrl, properties);
return connection;
}