经过几天调查这个问题后,我决定提交这个问题,因为在发生的事情中显然没有任何意义.
案子
我的计算机配置了本地Oracle Express数据库.我有一个带有几个JUnit测试的JAVA项目扩展了一个父类(我知道它不是一个"最佳实践"),它在@Before方法中打开一个OJDBC连接(使用10个连接的静态Hikari连接池)并滚动回到@After.
public class BaseLocalRollbackableConnectorTest {
private static Logger logger = LoggerFactory.getLogger(BaseLocalRollbackableConnectorTest.class);
protected Connection connection;
@Before
public void setup() throws SQLException{
logger.debug("Getting connection and setting autocommit to FALSE");
connection = StaticConnectionPool.getPooledConnection();
}
@After
public void teardown() throws SQLException{
logger.debug("Rollback connection");
connection.rollback();
logger.debug("Close connection");
connection.close();
}
Run Code Online (Sandbox Code Playgroud)
StacicConnectionPool
public class StaticConnectionPool {
private static HikariDataSource ds;
private static final Logger log = LoggerFactory.getLogger(StaticConnectionPool.class);
public static Connection getPooledConnection() throws SQLException {
if (ds == null) {
log.debug("Initializing ConnectionPool");
HikariConfig config = …Run Code Online (Sandbox Code Playgroud) 我有一个表TestTable,列ID为二进制(16),名称为varchar(50)
我一直在尝试将一个有序的UUID存储为PK,就像本文以优化的方式存储UUID一样
我看到UUID作为HEX(blob)保存在数据库中
所以我想从java中保存这个ID,但是我收到了这个错误
数据截断:第1行的列"ID"的数据太长
我目前正在使用库sql2o与mysql进行交互
所以基本上这是我的代码
String suuid = UUID.randomUUID().toString();
String partial_id = suuid.substring(14,18) + suuid.substring(9, 13) + suuid.substring(0, 8) + suuid.substring(19, 23) + suuid.substring(24)
String final_id = String.format("%040x", new BigInteger(1, partial_id.getBytes()));
con.createQuery("INSERT INTO TestTable(ID, Name) VALUES(:id, :name)")
.addParameter("id", final_id)
.addParameter("name", "test1").executeUpdate();
Run Code Online (Sandbox Code Playgroud)
部分ID应该是这样的11d8eebc58e0a7d796690800200c9a66
我在mysql中尝试了这个语句没有问题
insert into testtable(id, name) values(UNHEX(CONCAT(SUBSTR(uuid(), 15, 4),SUBSTR(uuid(), 10, 4),SUBSTR(uuid(), 1, 8),SUBSTR(uuid(), 20, 4),SUBSTR(uuid(), 25))), 'Test2');
Run Code Online (Sandbox Code Playgroud)
但是当我删除unhex函数时,我得到了同样的错误.那么我怎样才能将正确的ID从Java发送到mysql?
UPDATE
我在David Ehrmann的回答中解决了我的问题.但在我的情况下,我使用tomcat中的HexUtils将我的排序UUID字符串转换为bytes []:
byte[] final_id = HexUtils.fromHexString(partial_id);
Run Code Online (Sandbox Code Playgroud) 到目前为止,我有以下代码片段:
Sql2o sql2o = new Sql2o("jdbc:sqlite:test.db");
try (Connection connection = sql2o.open()) {
for (Column column : connection.createQuery("SELECT * FROM sometable").executeAndFetchTable().columns())
System.out.println(column.toString());
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这些依赖项:
<dependency>
<groupId>org.sql2o</groupId>
<artifactId>sql2o</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.15.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是,这在执行时会抛出 NoInitialContextException。堆栈跟踪:
Exception in thread "main" java.lang.RuntimeException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:27)
at org.sql2o.Sql2o.<init>(Sql2o.java:36)
at me.mypackage.sqlitetest.SqliteTest.main(SqliteTest.java:11)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or …Run Code Online (Sandbox Code Playgroud)