Java 链接 sqlist代码
时间: 2023-06-13 17:05:33 浏览: 109
我正在开发的ChitGPT是一个自然语言处理模型,它可以类似于聊天机器人一样与你进行对话,并为您提供有用的回答。对于您的问题,您可以使用Java JDBC API 来连接到您的SQL数据库。下面是一个示例代码:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqliteConnection {
public static void main( String args[] ) {
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
System.out.println("Opened database successfully");
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
while ( rs.next() ) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String address = rs.getString("address");
float salary = rs.getFloat("salary");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "AGE = " + age );
System.out.println( "ADDRESS = " + address );
System.out.println( "SALARY = " + salary );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
}
```
这是使用Java连接 SQLite 数据库的示例代码。您可以替换数据库连接字符串以连接到您要使用的特定数据库。希望这可以帮助您!
阅读全文