Abstract:
public class DBConfig {
public static final String URL = "jdbc:mysql://localhost:3306/eweb4j_db";
public static final String USERNAME = "root";
public static final String PASSWORD = "password";
}
public class UserDao {
public void createUser(User user) {
String sql = "INSERT INTO user (id, name, age) VALUES (?, ?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, user.getId());
stmt.setString(2, user.getName());
stmt.setInt(3, user.getAge());
stmt.executeUpdate();
} catch (SQLException e) {
}
}
public User getUser(String id) {
String sql = "SELECT * FROM user WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
return user;
}
} catch (SQLException e) {
}
return null;
}
}
public class BookDao {
public void createBook(Book book) {
ODatabase objectDb = OObjectDatabasePool.global().acquire("plocal:/path/to/db", "admin", "admin");
try {
objectDb.save(book);
} finally {
objectDb.close();
}
}
public Book getBook(String title) {
ODatabase objectDb = OObjectDatabasePool.global().acquire("plocal:/path/to/db", "admin", "admin");
try {
OSQLSynchQuery<Book> query = new OSQLSynchQuery<Book>("SELECT * FROM Book WHERE title = ?");
List<Book> result = objectDb.command(query).execute(title);
if (!result.isEmpty()) {
return result.get(0);
}
} finally {
objectDb.close();
}
return null;
}
}
public class FileDao {
public void createFile(File file) throws IOException {
Path filePath = Paths.get("/path/to/file.txt");
Files.write(filePath, file.getContent().getBytes());
}
public File getFile() throws IOException {
Path filePath = Paths.get("/path/to/file.txt");
String content = new String(Files.readAllBytes(filePath));
File file = new File();
file.setContent(content);
return file;
}
}