Dear all,
I'm newbie in Java/JSP. I have created project with Java Desktop for university lecture subject. But I still have one task, still with Java but in Web environment.
I have this class. This class is used to get connection to MySQL. I create it for Java GUI
import java.sql.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
public class Conn
{
public String PROP_FILE = "initial.ini";
public Conn() {
}
public Connection getConnection() throws SQLException {
Connection cn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Properties p2 = new Properties();
p2 = loadProperties(PROP_FILE);
String server;
String database;
String userID;
String password;
String url;
server = p2.getProperty("Server");
database = p2.getProperty("Database");
userID = p2.getProperty("UserID");
password = p2.getProperty("Password");
url = "jdbc:mysql://" + server + "/" + database;
cn = DriverManager.getConnection (url, userID, password);
return cn;
}
catch (SQLException se)
{
System.out.println(se.toString());
return null;
} catch (Exception ex)
{
System.out.println(ex.toString());
return null;
}
}
public static Properties loadProperties(String sFile) {
Properties p = new Properties();
try {
FileInputStream in = new FileInputStream(sFile);
p.load(in);
in.close();
} catch (IOException iOException) {
JOptionPane.showMessageDialog(null, iOException);
}
return p;
}
}
in all GUI forms, I only set this code :
void isiTable(String kataKunci)
{
String SQL = "";
SQL = "SELECT UserID, UserName, Status FROM masteruser ";
tabMode.setRowCount(0);
try
{
[B]Koneksi getCn = new Conn();
Connection cn = getCn.getConnection();[/B]
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(SQL);
while(rs.next())
{
String Code = rs.getString(1);
String Name = rs.getString(2);
String Status = rs.getString(3);
String[] data = {Code,Name,Status};
tabMode.addRow(data);
}
}
catch(SQLException e)
{
}
}
So how do I set that connection in all JSP pages with same class ?
Thanks,
Kusno.