parent
8e65ff12dc
commit
25b88059fe
@ -0,0 +1 @@
|
||||
DML.java
|
@ -0,0 +1,10 @@
|
||||
<component name="libraryTable">
|
||||
<library name="junit-4.12">
|
||||
<CLASSES>
|
||||
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
|
||||
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
@ -0,0 +1,10 @@
|
||||
<component name="libraryTable">
|
||||
<library name="libs">
|
||||
<CLASSES>
|
||||
<root url="file://$PROJECT_DIR$/libs" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
<jarDirectory url="file://$PROJECT_DIR$/libs" recursive="false" />
|
||||
</library>
|
||||
</component>
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,51 @@
|
||||
import java.sql.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DML {
|
||||
|
||||
// 数据库连接字符串
|
||||
private static final String URL = "jdbc:sqlite:D:/uml/db/dbuml.db3";
|
||||
|
||||
@Test
|
||||
public void create() {
|
||||
try (Connection conn = DriverManager.getConnection(URL);
|
||||
Statement stmt = conn.createStatement()) {
|
||||
|
||||
// 创建数据表的 SQL 语句
|
||||
String sql = "create table if not exists xslist (" +
|
||||
"id integer primary key AUTOINCREMENT not null ," +
|
||||
"sno text not null," +
|
||||
"sname text," +
|
||||
"lx1 integer," +
|
||||
"lx2 integer" +
|
||||
");";
|
||||
|
||||
// 执行 SQL 语句
|
||||
stmt.execute(sql);
|
||||
|
||||
System.out.println("数据表 创建成功");
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void deleteAll() {
|
||||
try (Connection conn = DriverManager.getConnection(URL);
|
||||
Statement stmt = conn.createStatement()) {
|
||||
|
||||
// 删除所有记录的 SQL 语句
|
||||
String sql = "DELETE FROM xslist";
|
||||
|
||||
// 执行 SQL 语句
|
||||
int result = stmt.executeUpdate(sql);
|
||||
System.out.println("删除了 " + result + " 条记录");
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
public class jdbc {
|
||||
public static void main(String[] args) {
|
||||
// 定义数据库连接参数
|
||||
String driver = "org.sqlite.JDBC"; // SQLite JDBC驱动
|
||||
String url = "jdbc:sqlite:./db/dbuml.db3"; // 数据库文件路径
|
||||
String user = ""; // SQLite不需要用户名
|
||||
String password = ""; // SQLite不需要密码
|
||||
|
||||
Connection conn = null;
|
||||
|
||||
try {
|
||||
// 1. 加载注册驱动
|
||||
Class.forName(driver);
|
||||
|
||||
// 2. 连接数据库
|
||||
conn = DriverManager.getConnection(url, user, password);
|
||||
|
||||
// 3. 输出数据库信息
|
||||
if (conn != null) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
System.out.println("数据库名称: " + metaData.getDatabaseProductName());
|
||||
System.out.println("数据库版本号: " + metaData.getDatabaseProductVersion());
|
||||
System.out.println("数据库URL: " + metaData.getURL());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 4. 关闭连接
|
||||
try {
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue