在学习下面部分的知识之前,您必须对 Java JDBC 概念有初步了解。如果您还未了解相关知识,那么建议您可以先花半个小时学习下 JDBC 教程相关知识,这将有助于您学习接下来讲解的知识。
package sqliteDemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class sqliteTest { PRivate static Connection c = null; private static Statement stmt = null; /** * @param args */ public static void main(String[] args) { try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db");//如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。 System.out.println("Opened database successfully"); createdTable(); //insert(); //select(); //update(); //delete(); stmt.close(); c.close(); } catch ( Exception e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); System.exit(0); } } public static void createdTable() throws SQLException{ 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); System.out.println("Table created successfully"); } public static void insert() throws SQLException{ c.setAutoCommit(false); 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); c.commit(); stmt.close(); System.out.println("Insert successfully"); } public static void select() throws SQLException{ c.setAutoCommit(false); 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.print( "ID = " + id ); System.out.print( "NAME = " + name ); System.out.print( "AGE = " + age ); System.out.print( "ADDRESS = " + address ); System.out.println( "SALARY = " + salary ); } rs.close(); } public static void update() throws SQLException{ c.setAutoCommit(false); stmt = c.createStatement(); String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=2;"; stmt.executeUpdate(sql); c.commit(); 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.print( "ID = " + id ); System.out.print( "NAME = " + name ); System.out.print( "AGE = " + age ); System.out.print( "ADDRESS = " + address ); System.out.println( "SALARY = " + salary ); } rs.close(); } public static void delete() throws SQLException{ c.setAutoCommit(false); stmt = c.createStatement(); String sql = "DELETE from COMPANY where ID=2;"; stmt.executeUpdate(sql); c.commit(); 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(); }}
新闻热点
疑难解答