yichen
2 years ago
28 changed files with 257 additions and 84 deletions
@ -1,7 +1,7 @@ |
|||||||
package com.fanruan.jdbc.connection; |
package com.fanruan.agentjdbc.connection; |
||||||
|
|
||||||
import com.fanruan.jdbc.statement.MyPreparedStatement; |
import com.fanruan.agentjdbc.statement.MyPreparedStatement; |
||||||
import com.fanruan.jdbc.statement.MyStatement; |
import com.fanruan.agentjdbc.statement.MyStatement; |
||||||
|
|
||||||
import java.sql.*; |
import java.sql.*; |
||||||
import java.util.Map; |
import java.util.Map; |
@ -1,6 +1,6 @@ |
|||||||
package com.fanruan.jdbc.driver; |
package com.fanruan.agentjdbc.driver; |
||||||
|
|
||||||
import com.fanruan.jdbc.connection.MyConnection; |
import com.fanruan.agentjdbc.connection.MyConnection; |
||||||
|
|
||||||
import java.sql.*; |
import java.sql.*; |
||||||
import java.util.Enumeration; |
import java.util.Enumeration; |
@ -1,4 +1,4 @@ |
|||||||
package com.fanruan.jdbc.resultset; |
package com.fanruan.agentjdbc.resultset; |
||||||
|
|
||||||
import java.io.InputStream; |
import java.io.InputStream; |
||||||
import java.io.Reader; |
import java.io.Reader; |
@ -1,7 +1,7 @@ |
|||||||
package com.fanruan.jdbc.statement; |
package com.fanruan.agentjdbc.statement; |
||||||
|
|
||||||
|
|
||||||
import com.fanruan.jdbc.resultset.MyResultSet; |
import com.fanruan.agentjdbc.resultset.MyResultSet; |
||||||
|
|
||||||
import java.io.InputStream; |
import java.io.InputStream; |
||||||
import java.io.Reader; |
import java.io.Reader; |
@ -1,6 +1,6 @@ |
|||||||
package com.fanruan.jdbc.statement; |
package com.fanruan.agentjdbc.statement; |
||||||
|
|
||||||
import com.fanruan.jdbc.resultset.MyResultSet; |
import com.fanruan.agentjdbc.resultset.MyResultSet; |
||||||
|
|
||||||
import java.sql.*; |
import java.sql.*; |
||||||
|
|
@ -1,7 +1,7 @@ |
|||||||
package com.fanruan.jdbc; |
package com.fanruan.servicejdbc; |
||||||
|
|
||||||
|
|
||||||
import com.fanruan.jdbc.driver.MyDriver; |
import com.fanruan.servicejdbc.driver.MyDriver; |
||||||
|
|
||||||
import java.sql.Connection; |
import java.sql.Connection; |
||||||
import java.sql.ResultSet; |
import java.sql.ResultSet; |
@ -1,11 +1,11 @@ |
|||||||
package com.fanruan.jdbc.connection; |
package com.fanruan.servicejdbc.connection; |
||||||
|
|
||||||
|
|
||||||
import com.corundumstudio.socketio.SocketIOClient; |
import com.corundumstudio.socketio.SocketIOClient; |
||||||
import com.fanruan.cache.ClientCache; |
import com.fanruan.cache.ClientCache; |
||||||
import com.fanruan.jdbc.MyDataBaseMetaData; |
import com.fanruan.servicejdbc.MyDataBaseMetaData; |
||||||
import com.fanruan.jdbc.statement.MyPreparedStatement; |
import com.fanruan.servicejdbc.statement.MyPreparedStatement; |
||||||
import com.fanruan.jdbc.statement.MyStatement; |
import com.fanruan.servicejdbc.statement.MyStatement; |
||||||
import com.fanruan.proxy.ProxyFactory; |
import com.fanruan.proxy.ProxyFactory; |
||||||
|
|
||||||
import java.sql.*; |
import java.sql.*; |
@ -1,6 +1,6 @@ |
|||||||
package com.fanruan.jdbc.driver; |
package com.fanruan.servicejdbc.driver; |
||||||
|
|
||||||
import com.fanruan.jdbc.connection.MyConnection; |
import com.fanruan.servicejdbc.connection.MyConnection; |
||||||
import com.fanruan.proxy.ProxyFactory; |
import com.fanruan.proxy.ProxyFactory; |
||||||
|
|
||||||
import java.sql.*; |
import java.sql.*; |
@ -1,4 +1,4 @@ |
|||||||
package com.fanruan.jdbc.resultset; |
package com.fanruan.servicejdbc.resultset; |
||||||
|
|
||||||
|
|
||||||
import java.io.InputStream; |
import java.io.InputStream; |
@ -1,6 +1,6 @@ |
|||||||
package com.fanruan.jdbc.statement; |
package com.fanruan.servicejdbc.statement; |
||||||
|
|
||||||
import com.fanruan.jdbc.resultset.MyResultSet; |
import com.fanruan.servicejdbc.resultset.MyResultSet; |
||||||
import com.fanruan.proxy.ProxyFactory; |
import com.fanruan.proxy.ProxyFactory; |
||||||
|
|
||||||
import java.io.InputStream; |
import java.io.InputStream; |
@ -1,6 +1,6 @@ |
|||||||
package com.fanruan.jdbc.statement; |
package com.fanruan.servicejdbc.statement; |
||||||
|
|
||||||
import com.fanruan.jdbc.resultset.MyResultSet; |
import com.fanruan.servicejdbc.resultset.MyResultSet; |
||||||
import com.fanruan.proxy.ProxyFactory; |
import com.fanruan.proxy.ProxyFactory; |
||||||
|
|
||||||
import java.sql.*; |
import java.sql.*; |
@ -0,0 +1,68 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<parent> |
||||||
|
<artifactId>intranet</artifactId> |
||||||
|
<groupId>org.example</groupId> |
||||||
|
<version>1.0-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
|
||||||
|
<artifactId>test</artifactId> |
||||||
|
|
||||||
|
<properties> |
||||||
|
<maven.compiler.source>8</maven.compiler.source> |
||||||
|
<maven.compiler.target>8</maven.compiler.target> |
||||||
|
</properties> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.example</groupId> |
||||||
|
<artifactId>service</artifactId> |
||||||
|
<version>1.0-SNAPSHOT</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.example</groupId> |
||||||
|
<artifactId>agent</artifactId> |
||||||
|
<version>1.0-SNAPSHOT</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> |
||||||
|
<dependency> |
||||||
|
<groupId>org.junit.jupiter</groupId> |
||||||
|
<artifactId>junit-jupiter-engine</artifactId> |
||||||
|
<version>5.4.0</version> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.logging.log4j</groupId> |
||||||
|
<artifactId>log4j-api</artifactId> |
||||||
|
<version>2.17.2</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.logging.log4j</groupId> |
||||||
|
<artifactId>log4j-core</artifactId> |
||||||
|
<version>2.17.2</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb --> |
||||||
|
<dependency> |
||||||
|
<groupId>org.hsqldb</groupId> |
||||||
|
<artifactId>hsqldb</artifactId> |
||||||
|
<classifier>debug</classifier> |
||||||
|
<version>2.5.2</version> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.logging.log4j</groupId> |
||||||
|
<artifactId>log4j-slf4j-impl</artifactId> |
||||||
|
<version>2.17.2</version> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
</project> |
@ -0,0 +1,110 @@ |
|||||||
|
|
||||||
|
import com.fanruan.AgentStarter; |
||||||
|
import com.fanruan.ServerStater; |
||||||
|
|
||||||
|
import com.fanruan.servicejdbc.driver.MyDriver; |
||||||
|
import com.fanruan.proxy.ProxyFactory; |
||||||
|
import com.fanruan.utils.DBProperties; |
||||||
|
import org.junit.jupiter.api.Assertions; |
||||||
|
import org.junit.jupiter.api.BeforeAll; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.sql.*; |
||||||
|
import java.util.Properties; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Yichen Dai |
||||||
|
* @date 2022/8/18 15:27 |
||||||
|
*/ |
||||||
|
public class TestUtil { |
||||||
|
|
||||||
|
@BeforeAll |
||||||
|
static void autoConfig(){ |
||||||
|
configService(); |
||||||
|
configAgent(); |
||||||
|
try { |
||||||
|
Thread.sleep(2000); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testCURD(){ |
||||||
|
Properties info = new Properties(); |
||||||
|
info.setProperty("user", "sa"); |
||||||
|
info.setProperty("password", ""); |
||||||
|
info.setProperty("agentID", "1001"); |
||||||
|
info.setProperty("agentDBName", DBProperties.HSQL[0]); |
||||||
|
|
||||||
|
Connection conn = null; |
||||||
|
Statement st = null; |
||||||
|
PreparedStatement pst = null; |
||||||
|
ResultSet rs = null; |
||||||
|
try { |
||||||
|
|
||||||
|
// 创建 连接
|
||||||
|
Driver driver = (MyDriver) ProxyFactory.getProxy(MyDriver.class, null); |
||||||
|
conn = driver.connect("jdbc:hsqldb:mem:test", info); |
||||||
|
|
||||||
|
// 创建 statement
|
||||||
|
st = conn.createStatement(); |
||||||
|
|
||||||
|
// 创建表
|
||||||
|
st.executeUpdate("DROP TABLE student IF EXISTS;"); |
||||||
|
|
||||||
|
st.executeUpdate("CREATE TABLE student (" + |
||||||
|
"student_id INTEGER GENERATED BY DEFAULT AS IDENTITY " + |
||||||
|
"(START WITH 1, INCREMENT BY 1) NOT NULL," + |
||||||
|
"student_name VARCHAR(100) NOT NULL," + |
||||||
|
"student_address VARCHAR(100) NOT NULL," + |
||||||
|
"PRIMARY KEY (student_id)" + |
||||||
|
");"); |
||||||
|
|
||||||
|
// 插入数据
|
||||||
|
st.executeUpdate("INSERT INTO student VALUES" + |
||||||
|
"(1, '张三', '上海')," + |
||||||
|
"(2, '李四', '北京')," + |
||||||
|
"(3, '王五', '成都');"); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 预查询语句 删除指定 ID
|
||||||
|
pst = conn.prepareStatement("delete from student where student_id = ?"); |
||||||
|
|
||||||
|
pst.setInt(1, 1); |
||||||
|
|
||||||
|
pst.executeUpdate(); |
||||||
|
|
||||||
|
rs = st.executeQuery("select * from student"); |
||||||
|
|
||||||
|
String[] nameStrings = new String[]{"张三", "李四", "王五"}; |
||||||
|
String[] addressStrings = new String[]{"上海", "北京", "成都"}; |
||||||
|
|
||||||
|
// 结果集断言
|
||||||
|
int num = 2; |
||||||
|
while(rs.next()) { |
||||||
|
Assertions.assertEquals(rs.getInt("student_id"), num); |
||||||
|
Assertions.assertEquals(rs.getString("student_name"), nameStrings[num-1]); |
||||||
|
Assertions.assertEquals(rs.getString("student_address"), addressStrings[num-1]); |
||||||
|
num++; |
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void configService(){ |
||||||
|
String[][] DBs = new String[][]{ |
||||||
|
DBProperties.HSQL, |
||||||
|
}; |
||||||
|
new ServerStater(DBs); |
||||||
|
} |
||||||
|
|
||||||
|
static void configAgent(){ |
||||||
|
String[][] DBs = new String[][]{ |
||||||
|
DBProperties.HSQL, |
||||||
|
}; |
||||||
|
new AgentStarter(DBs); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue