Use the ORMLite JDBC framework to perform data duration in the Java class library
Data persistence in the Java library is one of the common requirements in application development.ORMLITE is a lightweight object relationship mapping (ORM) library. It provides a way to simplify the database operation without writing a lengthy SQL query statement.This article will introduce how to use the ORMLITE JDBC framework to make data persistence in the Java class library and provide the corresponding Java code example.
First of all, you need to add a jar file to the ORMLITE library to the Java project.You can download the latest jar files from Oremlite's official website (http://ormlite.com/) and import it into your project.
Next, create a Java class to represent the data model.Suppose we want to manage a student object (Student), which contains ID, name (name), and age attributes.Use ORMLITE's annotation to mark these attributes in the class. The example is as follows:
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "students")
public class Student {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private String name;
@DatabaseField
private int age;
public Student() {
// Oremlite requires a non -parameter structure
}
// Getters and Setters are omitted ...
}
In the above example, the@databasetable annotation specifies the table name "Students", and the @DataBaseField annotation is used to mark the database field corresponding to the attribute.`generatedid = true` indicates that the field is the primary key that is automatically generated.
Next, we need to create a database connection and perform related operations.First, create a ORMLITE configuration object to configure the database connection information. The example is as follows:
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
public class DatabaseManager {
private static final String DATABASE_URL = "jdbc:sqlite:/path/to/database.db"; // 数据库URL
private static ConnectionSource connectionSource;
public static ConnectionSource getConnectionSource() {
if (connectionSource == null) {
try {
connectionSource = new JdbcConnectionSource(DATABASE_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
return connectionSource;
}
public static void closeConnectionSource() {
if (connectionSource != null) {
try {
connectionSource.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
In the above example, we use the JDBC connection URL to configure the database connection.You need to replace the `/path/to/database.db` with your own database path.
Next, database operations can be started.The following example shows how to add student objects to the database:
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import java.sql.SQLException;
public class StudentDao {
private Dao<Student, Integer> studentDao;
public StudentDao() {
try {
studentDao = DaoManager.createDao(DatabaseManager.getConnectionSource(), Student.class);
// If the table does not exist, create a table
DaoManager.createTableIfNotExists(DatabaseManager.getConnectionSource(), Student.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addStudent(Student student) {
try {
studentDao.create(student);
} catch (SQLException e) {
e.printStackTrace();
}
}
\ t // Other database operation methods are omitted ...
}
In the above example, we created a StudentDao class to pack database operations.In the constructive method, we use the method of `daomanager.createdao () to create a DAO object associated with the Student class to perform database operations.If the database table does not exist, use the method of `daomanager.createtableIfnotexist () to create a table.
Finally, you can use the StudentDao class to perform database operations in other parts of the application, such as:
public class Main {
public static void main(String[] args) {
StudentDao studentDao = new StudentDao();
// Create a student object
Student student = new Student();
Student.setname ("Zhang San");
student.setAge(20);
// Add student objects to database
studentDao.addStudent(student);
}
}
The above example shows how to use the StudentDao class to add student objects to the database.
Through the above steps, we have learned to use the ORMLITE JDBC framework for data persistence in the Java library.You can continue to explore the other functions and characteristics of ORMLITE according to your needs, such as querying, updating and deleting operations.