连接数据库#
import java.sql.*;
public class SQliteJDBC
{
public static void main(String args[])
{
Connection c = null;
try{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ":" + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
创建表#
import java.sql.*;
public class SQLiteJDBC
{
public static void main(String args[])
{
Connection c = null;
Statement stmt = null;
try{
Class.forName("org.sqlite.JDBC");
c = DriverManger.getConnection("jdbc:sqlite:test.db");
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "CREATE TABLE COMPANY " +
"(ID INT PRIMARY KEY NOT NULL, " +
"NAME TEXT NOT NULL," +
"AGE INT NOT NULL, " +
"ADDRESS CHAR(50), "+
"SALARY REAL)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch(Exception e) {
System.err.println(e.getClass().getName() + ":" + e.getMessage() );
System.exit(0);
}
System.out.println("Table created successfully");
}
}
INSERT操作#
import java.sql.*;
publc class SQLiteJDBC
{
Connection c = null;
Statement stmt = null;
try{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
stmt.executeUpdate(sql);
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );";
stmt.executeUpdate(sql); sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );";
stmt.executeUpdate(sql);
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
stmt.executeUpdate(sql); stmt.close(); c.commit(); c.close();
} catch(){
}
}
SELECT操作#
SELECT
UPDATE操作#
UPDATE
DELETE操作#
DELETE
网友评论