ammu
ammu

Reputation: 864

Connection changes at RUN TIME in JAVA app

I have to create a JSP in which there is a screen which makes DB connection at runtime, means i have to provide DB details (thin client / driver info etc) at run time and then connect and procced with next pages and this is a small scale appln (Max 20 users) so I want 2 know how can i manage connections.

-- Store connections in Session ? -- In HashTable with key as session id and value as connection ? -- or some connection pool at run time (i dont think it is good).. -- or create property file and make changes at runtime and reload it..

Let me know the best approach for this wrt performance and design.

Regards, Amit

Upvotes: 1

Views: 564

Answers (3)

Pratik Patel
Pratik Patel

Reputation: 174

you can set connection information(db Name,password,server Address,etc...) in .properties file. LIKE below.

1)static way config.properties(generally it created at classPath-src\config.properties) file contains

driver=com.mysql.jdbc.Driver
database=jdbc:mysql://localhost:3306/dbName
dbuser=root
dbpassword=root

you can also create or reset value

or 1)create or set dynamic properties file.

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class App 
{
    public static void main( String[] args )
    {
        Properties prop = new Properties();

        try {
            //set the properties value
            prop.setProperty("database", "localhost");
            prop.setProperty("dbuser", "patel");
            prop.setProperty("dbpassword", "password");

            //save properties to project root folder
            prop.store(new FileOutputStream("config.properties"), null);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

output in (config.properties)

 dbpassword=password
 database=localhost
 dbuser=patel

2)now time to read it at runtime.

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class App 
{
    public static void main( String[] args )
    {
        Properties prop = new Properties();

        try {
               //load a properties file
            prop.load(new FileInputStream("config.properties"));

               //get the property value and print it out
                System.out.println(prop.getProperty("database"));
            System.out.println(prop.getProperty("dbuser"));
            System.out.println(prop.getProperty("dbpassword"));

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

output

localhost
patel
password

pls write this code in jsp in <% %> this way u can load any thing at run time. need any help ping me..................:)

Upvotes: 4

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

It may be simpler to store login credentials in the server memory map that uses session id as a key, reopen the connection on each call and close it at the end of servicing the call. While there is a penalty to open a connection, one per web interaction should be efficient enough. This guards against connection leak.

To prevent forged sessions ids that would be a security problem, your web server must make sure that the user is authenticated for each call.

Upvotes: 0

Avinash Nair
Avinash Nair

Reputation: 2074

You can use connection pooling

check this

Upvotes: 0

Related Questions