@@ -36,7 +36,7 @@ For Maven:
36
36
<dependency >
37
37
<groupId >org.casbin</groupId >
38
38
<artifactId >jdbc-adapter</artifactId >
39
- <version >1.1.4 </version >
39
+ <version >2.0.0 </version >
40
40
</dependency >
41
41
```
42
42
@@ -48,24 +48,30 @@ package com.company.test;
48
48
import org.casbin.jcasbin.main.Enforcer ;
49
49
import org.casbin.jcasbin.util.Util ;
50
50
import org.casbin.adapter.JDBCAdapter ;
51
+ import com.mysql.cj.jdbc.MysqlDataSource ;
51
52
52
53
public class Test {
53
54
public static void main () {
54
- // Initialize a JDBC adapter and use it in a jCasbin enforcer:
55
- // The adapter will use the MySQL database named "casbin".
56
- // If it doesn't exist, the adapter will create it automatically.
57
- JDBCAdapter a = new JDBCAdapter ( " com.mysql.cj.jdbc.Driver " , " jdbc:mysql://localhost:3306/db_name " , " root " , " 123 " ); // Your driver and URL.
55
+ String driver = " com.mysql.cj.jdbc.Driver " ;
56
+ String url = " jdbc:mysql://localhost:3306/db_name " ;
57
+ String username = " root " ;
58
+ String password = " 123456 " ;
58
59
59
- // Or you can use an existing DB "abc" like this:
60
60
// The adapter will use the table named "casbin_rule".
61
- // If it doesn't exist, the adapter will create it automatically.
62
- // JDBCAdapter a = new JDBCAdapter("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/casbin", "root", "123", true);
61
+ // Use driver, url, username and password to initialize a JDBC adapter.
62
+ JDBCAdapter a = new JDBCAdapter (driver, url, username, password);
63
+
64
+ // Recommend use DataSource to initialize a JDBC adapter.
65
+ // Implementer of DataSource interface, such as hikari, c3p0, durid, etc.
66
+ MysqlDataSource dataSource = new MysqlDataSource ();
67
+ dataSource. setURL(url);
68
+ dataSource. setUser(username);
69
+ dataSource. setPassword(password);
70
+
71
+ a = JDBCAdapter(dataSource);
63
72
64
73
Enforcer e = new Enforcer (" examples/rbac_model.conf" , a);
65
74
66
- // Load the policy from DB.
67
- e. loadPolicy();
68
-
69
75
// Check the permission.
70
76
e. enforce(" alice" , " data1" , " read" );
71
77
0 commit comments