•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,674 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,261 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2405 | Replies: 1
![]() |
•
•
Join Date: Sep 2006
Posts: 65
Reputation:
Rep Power: 0
Solved Threads: 0
hi all,
i got this example from a website, but unfortunately i cant understand some cording in that source.
the green color lines are the difficult points that i could not understand.
also the database name, user name and password has not specified at anywhere in this cord.
how can i make this cord to run without problems....?
please help me.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
public class MainClass {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
// Do work with connection
statement = connection.createStatement();
String selectEmployeesSQL = "SELECT * FROM employees";
resultSet = statement.executeQuery(selectEmployeesSQL);
while (resultSet.next()) {
printEmployee(resultSet);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
}// nothing we can do
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
} // nothing we can do
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
} // nothing we can do
}
}
}
private static Connection getConnection() throws NamingException, SQLException {
InitialContext initCtx = createContext();
String jndiName = "HrDS";
ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
PooledConnection pooledConnection = dataSource.getPooledConnection();
return pooledConnection.getConnection(); // Obtain connection from pool
}
private static InitialContext createContext() throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext context = new InitialContext(env);
return context;
}
private static void printEmployee(ResultSet resultSet) throws SQLException {
System.out.print(resultSet.getInt("employee_id")+", ");
System.out.print(resultSet.getString("last_name")+", ");
System.out.print(resultSet.getString("first_name")+", ");
System.out.println(resultSet.getString("email"));
}
}
i got this example from a website, but unfortunately i cant understand some cording in that source.
the green color lines are the difficult points that i could not understand.
also the database name, user name and password has not specified at anywhere in this cord.
how can i make this cord to run without problems....?
please help me.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
public class MainClass {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
// Do work with connection
statement = connection.createStatement();
String selectEmployeesSQL = "SELECT * FROM employees";
resultSet = statement.executeQuery(selectEmployeesSQL);
while (resultSet.next()) {
printEmployee(resultSet);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
}// nothing we can do
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
} // nothing we can do
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
} // nothing we can do
}
}
}
private static Connection getConnection() throws NamingException, SQLException {
InitialContext initCtx = createContext();
String jndiName = "HrDS";
ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
PooledConnection pooledConnection = dataSource.getPooledConnection();
return pooledConnection.getConnection(); // Obtain connection from pool
}
private static InitialContext createContext() throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext context = new InitialContext(env);
return context;
}
private static void printEmployee(ResultSet resultSet) throws SQLException {
System.out.print(resultSet.getInt("employee_id")+", ");
System.out.print(resultSet.getString("last_name")+", ");
System.out.print(resultSet.getString("first_name")+", ");
System.out.println(resultSet.getString("email"));
}
}
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
If that's part of a JSP, never look at that example again.
That sort of code (or indeed ANY code) does NOT belong in a JSP.
Nor should anyone, ever, directly reference any of the classes in the com.sun packages.
This code references a connection pool provided by an application server.
The application server itself will hold the actual database configuration, including database URL, username, password, driverclass, etc. etc..
That sort of code (or indeed ANY code) does NOT belong in a JSP.
Nor should anyone, ever, directly reference any of the classes in the com.sun packages.
This code references a connection pool provided by an application server.
The application server itself will hold the actual database configuration, including database URL, username, password, driverclass, etc. etc..
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Hii, Need Some Help On Connection pooling (Java)
- Connection pooling in java (Java)
- java.net.SocketException MESSAGE: Connection reset (MySQL)
- WML and connection to Acess and JSP/tomcat (Computer Science and Software Design)
- Page hangs or displays connection pooling max size error (ASP.NET)
Other Threads in the Java Forum
- Previous Thread: Logical operations and data-checks
- Next Thread: Getting errors while compiling



Linear Mode