Skip to content

Commit 8989f06

Browse files
authored
#4 - Code sample (#8)
* #4 - Code sample - Version bump - Connection failure handle fix - Auth parameter pass fix * #4 Test comment * #4 Test comment * #4 Javadoc
1 parent a50252b commit 8989f06

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ Token based authentication supported in version 1.0. Pass token value as passwor
2121

2222
Driver class name: `com.dbeaver.jdbc.driver.libsql.LibSqlDriver`
2323

24+
## Example
25+
26+
```java
27+
import java.sql.*;
28+
29+
public class LibSqlTest {
30+
public static void main(String[] args) throws Exception {
31+
String databaseUrl = "http://libsql-server.company.local:8080";
32+
try (Connection connection = DriverManager.getConnection("jdbc:dbeaver:libsql:" + databaseUrl)) {
33+
try (Statement statement = connection.createStatement()) {
34+
statement.execute("drop table if exists test_table_1");
35+
statement.execute("create table test_table_1 (id integer, name string)");
36+
statement.execute("insert into test_table_1 values(1, 'test one')");
37+
statement.execute("insert into test_table_1 values(2, 'test two')");
38+
try (ResultSet rs = statement.executeQuery("select * from test_table_1")) {
39+
while (rs.next()) {
40+
System.out.println(rs.getInt("id") + " = " + rs.getString("name"));
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
```
2448
## License
2549

2650
Licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)

com.dbeaver.jdbc.driver.libsql/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: DBeaver LibSQL JDBC Driver
44
Bundle-SymbolicName: com.dbeaver.jdbc.driver.libsql
5-
Bundle-Version: 1.0.2.qualifier
5+
Bundle-Version: 1.0.3.qualifier
66
Bundle-Release-Date: 20230522
77
Bundle-RequiredExecutionEnvironment: JavaSE-17
88
Export-Package: com.dbeaver.jdbc.driver.libsql,

com.dbeaver.jdbc.driver.libsql/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.dbeaver.jdbc</groupId>
99
<artifactId>jdbc-libsql</artifactId>
10-
<version>1.0.2-SNAPSHOT</version>
10+
<version>1.0.3-SNAPSHOT</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313
<artifactId>com.dbeaver.jdbc.driver.libsql</artifactId>

com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConnection.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,23 @@ public LibSqlConnection(
5151
this.driverProperties = driverProperties;
5252

5353
try {
54-
String token = CommonUtils.toString(driverProperties.get("password"));
54+
String token = CommonUtils.toString(driverProperties.get("password"), null);
5555
this.client = new LibSqlClient(new URL(url), token);
5656
} catch (IOException e) {
5757
throw new SQLException(e);
5858
}
59-
// Verify connection
60-
LibSqlUtils.executeQuery(this, "SELECT 1");
59+
try {
60+
// Verify connection
61+
LibSqlUtils.executeQuery(this, "SELECT 1");
62+
} catch (Exception e) {
63+
close();
64+
throw e;
65+
}
6166
}
6267

68+
/**
69+
* Obtain transport client
70+
*/
6371
public LibSqlClient getClient() {
6472
String applicationName = getClientApplicationName();
6573
if (!CommonUtils.isEmpty(applicationName)) {

com.dbeaver.jdbc.driver.libsql/src/test/java/com/dbeaver/jdbc/upd/driver/test/LibSqlDriverTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717
package com.dbeaver.jdbc.upd.driver.test;
1818

1919
import java.sql.*;
20-
import java.util.Properties;
2120

2221
public class LibSqlDriverTest {
22+
/**
23+
* Runs simple select query in LibSQL
24+
*
25+
* @param args mvn exec:java "-Dexec.args=database-url [token]"
26+
*/
2327
public static void main(String[] args) throws Exception {
2428
long startTime = System.currentTimeMillis();
2529

2630
try {
27-
Properties props = new Properties();
28-
try (Connection connection = DriverManager.getConnection("jdbc:dbeaver:libsql:" + args[0], props)) {
31+
String databaseUrl = args[0];
32+
String token = args.length > 1 ? args[1] : null;
33+
try (Connection connection = DriverManager.getConnection("jdbc:dbeaver:libsql:" + databaseUrl, null, token)) {
2934
DatabaseMetaData metaData = connection.getMetaData();
3035
System.out.println("Driver: " + metaData.getDriverName());
3136
System.out.println("Database: " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.dbeaver.jdbc</groupId>
88
<artifactId>jdbc-libsql</artifactId>
9-
<version>1.0.2-SNAPSHOT</version>
9+
<version>1.0.3-SNAPSHOT</version>
1010

1111
<packaging>pom</packaging>
1212
<name>DBeaver LibSQL JDBC Project</name>

0 commit comments

Comments
 (0)