package id.ac.poliban.roman.jdbc.preparedstatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
* This code is used to demonstrate the use of JDBC class for mysql(mysql-connector-java)
* in manipulating sqlite tables. This code uses the POJO Friend
* class, Interface Friend and a class that implements JDBC to manipulate the
* the mysql database. In this code we only use the java.sql.Statement class to
* manipulate the data.
*
* remember this code using jdk 8 and above
*
* @author antilaila8
*
*/
/**
* Notes :
* This tutorial using mysql database with a database that called romandb
* (if you don't have that database please create it) and
* a table that called friend. The structure friend table is :
*
* +---------+--------------+------+-----+---------+----------------+
* | Field | Type | Null | Key | Default | Extra |
* +---------+--------------+------+-----+---------+----------------+
* | id | int(11) | NO | PRI | NULL | auto_increment |
* | name | varchar(50) | NO | | | |
* | address | varchar(100) | NO | | | |
* | phone | varchar(14) | YES | | NULL | |
* +---------+--------------+------+-----+---------+----------------+
*
* if you don't have a table, please execute script sql below to create friend table :
*
* create table if not exists friend(
* id int not null auto_increment primary key,
* name varchar(20) not null,
* address varchar(30) not null,
* phone varchar(14) null)
*
* using method execSQL(sql) from object of FriendJDBCMySQLPreparedStatementTemplate
*
*/
public class FriendJDBCMySQLUsingStatementDemo {
public static void main
(String[] args
) {
String url
= "jdbc:mysql://localhost:3306/romandb?user=root&password=1234";
FriendJDBCMySQLStatementTemplate db = new FriendJDBCMySQLStatementTemplate(url);
//delete all record &
//set auto_increment 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 JDBC class
* please download mysql-connector-java
* or add dependencies in maven's pom.xml, see
* https://mvnrepository.com/artifact/mysql/mysql-connector-java
*
*/
class FriendJDBCMySQLStatementTemplate implements FriendDao {
//constructor
public FriendJDBCMySQLStatementTemplate
(String driver,
String url
) {
try {
Class.forName(driver);
System.
out.
println("Error: " + ex.
getMessage());
}
}
try {
Class.forName(driver);
System.
out.
println("Error: " + ex.
getMessage());
}
}
public FriendJDBCMySQLStatementTemplate
(String url
) {
try {
System.
out.
println("Error: " + ex.
getMessage());
}
}
@Override
public void execSQL
(String sql
) {
try ( Statement st
= con.
createStatement()) {
st.executeUpdate(sql);
System.
out.
println("Error : " + ex.
getMessage());
}
}
@Override
public void insert(Friend f) {
if (f.getId() == 0) {//when id is null
sql
= String.
format("insert into friend values(null, '%s', '%s', '%s')", f.
getName(), f.
getAddress(), f.
getPhone());
} else {
sql
= String.
format("insert into friend values('%d', '%s', '%s', '%s')", f.
getId(), f.
getName(), f.
getAddress(), f.
getPhone());
}
try ( Statement st
= con.
createStatement()) {
st.executeUpdate(sql);
System.
out.
println("Error : " + ex.
getMessage());
}
}
@Override
public void update(Friend f) {
String sql
= String.
format("update friend set name='%s', address='%s', phone = '%s' where id = '%d'",
f.getName(), f.getAddress(), f.getPhone(), f.getId());
try ( Statement st
= con.
createStatement()) {
st.executeUpdate(sql);
System.
out.
println("Error : " + ex.
getMessage());
}
}
@Override
public void delete(int id) {
String sql
= String.
format("delete from friend where id = '%d'", id
);
try ( Statement st
= con.
createStatement()) {
st.executeUpdate(sql);
System.
out.
println("Error : " + ex.
getMessage());
}
}
@Override
public void deleteAll() {
st.executeUpdate("delete from friend");
System.
out.
println("Error : " + ex.
getMessage());
}
st.executeUpdate("alter table friend auto_increment = 0");
System.
out.
println("Error : " + ex.
getMessage());
}
}
@Override
public Friend getAFriendById(int id) {
Friend result = null;
String sql
= String.
format("select * from friend where id = '%d'", id
);
if (rs.first()) {
result = new Friend(
rs.getInt("id"),
rs.getString("name"),
rs.getString("address"),
rs.getString("phone")
);
}
System.
out.
println("Error : " + ex.
getMessage());
}
return result;
}
@Override
public List<Friend> getAllFriends() {
List<Friend> result = new ArrayList<>();
String sql
= "select * from friend";
while(rs.next())
result.add(new Friend(
rs.getInt("id"),
rs.getString("name"),
rs.getString("address"),
rs.getString("phone")
));
System.
out.
println("Error : " + ex.
getMessage());
}
return result;
}
}