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.

Recommended Answers

All 7 Replies

You don't need any JDBC connections in JSP.
And you certainly don't want to keep a connection open past the lifetime of a single http request.

You don't need any JDBC connections in JSP.
And you certainly don't want to keep a connection open past the lifetime of a single http request.

I think I know what you mean.
But, I only wonder how I declare my class Conn in JSP page ?
So I don't have to write these code in every pages

Connection cn = null;
        try {            
            Class.forName("com.mysql.jdbc.Driver");     
            String server;
            String database;
            String userID;
            String password;
            String url;
            
            server = "localhost:3306;
            database = "mydb";
            userID = "root";
            password = "justEnter";
            
            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;
        }

Please, please, do not do any connection to database from JSP, it is bad thing. JSP is for presentation purposes, it is servlet that should be used for logic (on server validation, database communication)

I read from some JSP books and their connection put it JSP page and I think it's not flexible.
So I'm still anxious if there is any solution to solve my question.

If Peter said, I should use servlet, do you have example or link for me to learn ?

Thank,

Kusno

NB : Sorry for my mistake by submitting this thread I don't use Toggle Plain Text for coding.
:$

Servlet connection to db examples, and something to read on Java and web development The Java EE 5 Tutorial, I'm sure that jwenting know some other resources that are good for java beginners

For beginners, the best advice is to stay away from enterprise technologies until they have the basics down solid.

For those who insist, Oracle has a large number of tutorials on their OTN website. They're however closely linked to their own JDeveloper product, just like most new Sun tutorials are closely linked to Netbeans.

No matter what kind of tutorials you read, learning from a good book is almost always irreplaceable. For JSP/Servlets, Head First Servlets and JSP is a good choice IMO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.