database problems, which would make the program more
dynamic. This time, I will cite a brief skript
about how to create a database connection (JDBC) in the
java programming. DBMS used in this example is
MySql database. In my opinion, this is a MySQL connection
most easily done because they do not require configuration
complicated.
MySql default port is 3306.
Each DBMS has a port that each is different.
In PostgreSQL, the default port is 5432,
the SQLServer2000, the default port is 1433.
And this is the code :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class MemberDAOImpl implements MemberDAO {
private Connection connection;
public MemberDAOImpl() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/bank";
connection = DriverManager.getConnection(url, "root",
"");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
In order to make the connection can be called many times in other classes, add this script
public Connection getConnection(){
return connection;
}
For SQL scripts example, suppose a table is a 'member'.
This script can be made if the class for the entity member is made
first. If you need a file, you can contact me, I will send via email. And it so much easier to detect errors if we use the NetBeans application.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class MemberDAOImpl implements MemberDAO {
private Connection connection;
public MemberDAOImpl() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/bank";
connection = DriverManager.getConnection(url, "root",
"");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
In order to make the connection can be called many times in other classes, add this script
public Connection getConnection(){
return connection;
}
For SQL scripts example, suppose a table is a 'member'.
This script can be made if the class for the entity member is made
first. If you need a file, you can contact me, I will send via email. And it so much easier to detect errors if we use the NetBeans application.
public List getAllMember() {
List listMember = new ArrayList
Connection conn;
Statement sttmt;
try {
conn = getConnection();
sttmt = conn.createStatement();
String query = "select * from member";
ResultSet rsMember = sttmt.executeQuery(query);
while(rsMember.next()){
Member member = new Member();
member.setRegNumber(rsMember.getString(1));
member.setId(rsMember.getString(2));
member.setName(rsMember.getString(3));
member.setGender(rsMember.getInt(4));
member.setClas(rsMember.getString(5));
member.setBirthPlaceDate(rsMember.getString(6));
member.setAddress(rsMember.getString(7));
member.setPhoneNumber(rsMember.getString(8));
listMember.add(member);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
return listMember;
}
That's the script to connect to MySql, for other DBMS I will discuss in the next examples. Good luck & Happy coding ^ _ ^!
By: Mr.Stanza