import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBDemo {
private Connection connection;
private Statement statement;
public DBDemo() {
try {
connection = DriverManager.getConnection("jdbc:sqlite:Test.db");
statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void showInfo() {
ResultSet rs;
//System.out.println("show all info in DB");
try {
rs = statement.executeQuery("select * from stuinfo");
int i=0;
while(rs.next()) {
// read the result set
System.out.print("stuID = " + rs.getInt("stuID"));
System.out.print(". name = " + rs.getString("name"));
System.out.println(". cardID = " + rs.getString("cardID"));
i=i+1;
}
if(i==0) {
System.out.println("DB is empty.");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteInfo() {
try {
statement.executeUpdate("delete from stuinfo");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertInfo() {
try {
statement.executeUpdate("insert into stuinfo values(1150299888, 'root','0C67F171')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void changeInfo() {
try {
statement.executeUpdate("update stuinfo set cardID='11223344' where stuID=1150299888");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DBDemo d=new DBDemo();
System.out.println("original info");
d.showInfo();
d.deleteInfo();
System.out.println("after deletion");
d.showInfo();
d.insertInfo();
System.out.println("after insertion");
d.showInfo();
d.changeInfo();
System.out.println("after update");
d.showInfo();
}
}