package id.ac.poliban.roman.jdbc.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This code is used to demonstrate the use of JDBC SQLite (sqlite-jdbc version
* 3.36.0.3) in manipulating sqlite tables. This code uses the POJO Friend
* class, Interface Friend and a class that implements JDBC to manipulate the
* the sqlite database. In this code we only use the java.sql.PreparedStatement class to
* manipulate the data. See the next tutorial which will use the
* java.sql.PreparedStatement class
*
* remember this code using jdk 8 and above
*
* @author antilaila8
*
*/
public class FriendJDBCSQLiteDemo2 {
public static void main
(String[] args
) {
FriendJDBCSQLitePreparedStatementTemplate db = new FriendJDBCSQLitePreparedStatementTemplate();
//delete all record
//and reset autoincrement of friend table to 0
db.deleteAll();
//create list of Friend for insert
List<Friend> lsfriend1 = new ArrayList<>();
lsfriend1.add(new Friend("Ullah", "BJM", "0822511215"));
lsfriend1.add(new Friend("Eson", "BJM", "0822511216"));
lsfriend1.add(new Friend("Hing", "BJM", "0822511219"));
lsfriend1.add(new Friend("Johny Fren", "BJM", "0822511210"));
lsfriend1.add(new Friend("Acuna", "BJM", "0822511217"));
lsfriend1.add(new Friend("Fahmi Malaya", "BJM", "0822511213"));
//insert list to table
lsfriend1.forEach(f->db.insert(f));
//read data
System.
out.
println("read data after insert:");
db.
getAllFriends().
forEach(System.
out::println
);
//create list of Friend for update
List<Friend> lsFriend2 = new ArrayList<>();
lsFriend2.add(new Friend(3, "Ullah", "BJM", "0822511215"));
lsFriend2.add(new Friend(6,"Eson", "BJM", "0822511216"));
lsFriend2.add(new Friend(1, "Hing", "BJM", "0822511219"));
lsFriend2.add(new Friend(2, "Johny Fren", "BJM", "0822511210"));
lsFriend2.add(new Friend(5,"Acuna", "BJM", "0822511217"));
lsFriend2.add(new Friend(4, "Fahmi Malaya", "BJM", "0822511213"));
//update list to table
lsFriend2.forEach(f->db.update(f));
System.
out.
println("\n\nread data after update");
db.
getAllFriends().
forEach(System.
out::println
);
db.delete(3);
System.
out.
println("\n\nread data after delete id:3");
db.
getAllFriends().
forEach(System.
out::println
);
}
}
//the POJO class : Friend
class Friend{
private int id;
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
this.name = name;
this.address = address;
this.phone = phone;
}
public int getId() {
return id;
}
return name;
}
return address;
}
return phone;
}
@Override
return String.
format("%-3d %-20s %-30s %-14s", getId
(), getName
(), getAddress
(), getPhone
());
}
}
//FriendDao interface
interface FriendDao{
void insert(Friend f);
void update(Friend f);
void delete(int id);
void deleteAll();
Friend getAFriendById(int id);
List<Friend> getAllFriends();
}
/**
* class to implement SQLite JDBC
* please download sqlite-jdbc
* or add dependencies in maven's pom.xml, see
* https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.36.0.3
*
*/
//class SQLite JDBC Implementation
class FriendJDBCSQLitePreparedStatementTemplate implements FriendDao{
private final String DRIVER
= "org.sqlite.JDBC";
private final String URL = "jdbc:sqlite:antilaila8.db"; //database is antilaila8.db
//sql to make friend table
private final String sqlFriendTable
= "create table if not exists friend("
+ "id integer primary key autoincrement,"
+ "name text,"
+ "address text,"
+ "phone text)";
public FriendJDBCSQLitePreparedStatementTemplate() {
try {
Class.forName(DRIVER);
createTable(sqlFriendTable);
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void insert(Friend f) {
String sql
= "insert into friend values(?,?,?,?)";
if(f.getId()==0){
pst.setNull(1, f.getId());
pst.setString(2, f.getName());
pst.setString(3, f.getAddress());
pst.setString(4, f.getPhone());
}
else{
pst.setInt(1, f.getId());
pst.setString(2, f.getName());
pst.setString(3, f.getAddress());
pst.setString(4, f.getPhone());
}
pst.executeUpdate();
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void update(Friend f) {
String sql
= "update friend set name=?, address=?, phone=? where id=?";
pst.setString(1, f.getName());
pst.setString(2, f.getAddress());
pst.setString(3, f.getPhone());
pst.setInt(4, f.getId());
pst.executeUpdate();
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void delete(int id) {
String sql
= "delete from friend where id=?";
pst.setInt(1, id);
pst.executeUpdate();
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex.getMessage());
}
}
@Override
public void deleteAll() {
pst.executeUpdate();
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
//reset autoincrement to 0 from friend table
try(PreparedStatement pst
= con.
prepareStatement("update sqlite_sequence set seq = 0 where name = 'friend'")){
pst.executeUpdate();
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void createTable
(String sql
) {
pst.executeUpdate();
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public Friend getAFriendById(int id) {
Friend result = null;
String sql
= "select * from friend where id=?";
pst.setInt(1, id);
if(rs.first())
result = new Friend(
rs.getInt("id"),
rs.getString("name"),
rs.getString("address"),
rs.getString("phone")
);
}
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
@Override
public List<Friend> getAllFriends() {
List<Friend> result = new ArrayList<>();
while(rs.next())
result.add(new Friend(
rs.getInt("id"),
rs.getString("name"),
rs.getString("address"),
rs.getString("phone")
));
Logger.getLogger(FriendJDBCSQLitePreparedStatementTemplate.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
}