我正在尝试有一个简单的 java springboot 应用程序,它通过 JDBC 驱动程序(interbase)与数据库对话。
这是编译AND成功连接和查询数据库的代码:
package com.example.demo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zaxxer.hikari.HikariDataSource;
@RestController
public class HelloController {
final String DB_URL = "jdbc:interbase://0.0.0.0:3050/C:\\MYDB.GBB";
final String USER = ".....";
final String PASS = ".....";
final String QUERY = "SELECT * FROM USERS WHERE ID = 1";
@RequestMapping("/")
String hello() {
try {
// // load and register JDBC driver for MySQL
Class.forName("interbase.interclient.Driver");
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(DB_URL);
ds.setUsername(USER);
ds.setPassword(PASS);
// Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
System.out.print("USERID: " + rs.getInt("ID"));
System.out.print("USERNAME: " + rs.getString("USERNAME"));
System.out.println("CUSTID: " + rs.getString("CUSTID"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return "Hello World, Spring Boot!";
}
}
然而,
当我尝试通过配置 bean 将应用程序配置为使用“DataSource”时,出现错误:
“java.lang.ClassNotFoundException:无法加载请求的类:org.hibernate.dialect.InterbaseDialect”
这是我的数据源 bean:
package com.example.demo.spring.config;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
// @PropertySource("classpath:database.properties")
public class DataSourceBean {
@Value("${target.database}")
private String database;
@Value("${target.server}")
private String server;
@Value("${target.charset}")
private String charset;
@Value("${target.port}")
private String port;
@Value("${target.username}")
private String username;
@Value("${target.password}")
private String password;
@Bean
public DataSource interbaseDataSource() {
DataSource dataSource = new DataSource();
dataSource.setDatabaseName(database);
dataSource.setServerName(server);
dataSource.setCharSet(charset);
dataSource.setPortNumber(Integer.parseInt(port));
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
我尝试添加到 application.properties 的不同变体:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.InterbaseDialect
spring.jpa.database-platform=org.hibernate.dialect.InterbaseDialect
hibernate.dialect=org.hibernate.dialect.InterbaseDialect
但没有成功。
非常感谢任何提示,谢谢!