sqlite資料庫操作(雜但全)
阿新 • • 發佈:2020-01-29
package study; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import javax.naming.spi.DirStateFactory.Result; public class Sqlite { public static void main(String[] args) throws SQLException,ClassNotFoundException { // TODO Auto-generated method stub Connection cn=null; Statement st=null; //載入驅動 Class.forName("org.sqlite.JDBC"); //連線資料庫 cn=DriverManager.getConnection("jdbc:sqlite:Student.db"); //例項化statement st=cn.createStatement(); //輸入語句 //Scanner sc=new Scanner(System.in); //int ID=sc.nextInt(); //刪除語句 //String sql="DELETE FROM STUDENT WHERE ID=100"; //更新語句 //String sql="UPDATE STUDENT SET ID=1000 WHERE ID="+ID; //查詢語句 String sql="SELECT * FROM STUDENT;"; //結果集 ResultSet r=null; r=st.executeQuery(sql); //讀取每一行 while(r.next()) { int id=r.getInt("ID"); System.out.println(id); } //編寫語句 //建表 //String sql="CREATE TABLE STUDENT (ID INT PRIMARY KEY NOT NULL,NAME CHAR(20) " // +"NOT NULL,SEX CHAR(10) NOT NULL,CLASS CHAR(10) NOT NULL)"; //statement例項化 //執行語句 //st.executeUpdate(sql); //多個語句 // String sqllist[]= { // "INSERT INTO STUDENT VALUES(105,'YANGWEN','男','一班')",// "INSERT INTO STUDENT VALUES(106,'一班')" // }; //執行多個語句 // for(String sqll:sqllist) // { // st.executeUpdate(sqll); // } //st.execute(sql); cn.close(); } }