the SID you gave it to the database of data when creating it, it is the name of the database.
This is an example to connect through localhost:
import java.sql.*;
public class OracleConnection {
public static void main(String args[]) {
String usuario = "nombre_usuario";
String password = "secreta";
String host = "localhost"; // tambien puede ser una ip
String puerto = "1521";
String sid = "prueba";
String driver = "oracle.jdbc.driver.OracleDriver";
String urljdbc = "jdbc:oracle:thin:" + usuario + "/" + password + "@" + host + ":" + puerto + ":" + sid;
Connection connection = null;
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(urljdbc);
// Ya tenemos el objeto connection creado
ResultSet result = connection.createStatement().executeQuery("SELECT 'hola mundo' FROM DUAL");
result.next();
System.out.println(result.getString(1));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
}
}
Although the correct way is to connect through jdbc, that is, a pool of connections.
Go into GlassFish, create a pool of connections and the resource.
In your web project, in the web.xml file, you have to add this:
<listener>
<description>ServletContextListener</description>
<listener-class>Listener.Oyente</listener-class>
</listener>
<resource-ref>
<description>jdbc:oracle:thin:@localhost:1521:orcl</description>
<res-ref-name>ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Create a file glassfish-resources.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/oracle" pool-name="ORCL">
<description/>
</jdbc-resource>
<jdbc-connection-pool
allow-non-component-callers="false"
associate-with-thread="false"
connection-creation-retry-attempts="0"
connection-creation-retry-interval-in-seconds="10"
connection-leak-reclaim="false"
connection-leak-timeout-in-seconds="0"
connection-validation-method="auto-commit"
datasource-classname="oracle.jdbc.pool.OracleDataSource"
fail-all-connections="false"
idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true"
lazy-connection-association="false"
lazy-connection-enlistment="false"
match-connections="false"
max-connection-usage-count="0"
max-pool-size="32"
max-wait-time-in-millis="60000"
name="ORCL"
non-transactional-connections="false"
ping="false"
pool-resize-quantity="2"
pooling="true"
res-type="javax.sql.DataSource"
statement-cache-size="0"
statement-leak-reclaim="false"
statement-leak-timeout-in-seconds="0"
statement-timeout-in-seconds="-1"
steady-pool-size="8"
validate-atmost-once-period-in-seconds="0"
wrap-jdbc-objects="false">
<property name="URL" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="User" value="NOMBRE_USUARIO"/>
<property name="Password" value="PASSWORD"/>
</jdbc-connection-pool>
</resources>
Create a Model class:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
public class Modelo {
private DataSource ds;
public Modelo(DataSource ds) {
this.ds = ds;
}
public boolean existeUsuario(String USUARIO, String CONTRASENA) {
Connection con = null;
boolean resultado = true;
try {
con = ds.getConnection();
resultado = false;
PreparedStatement ps = con.prepareStatement("SELECT * FROM USUARIOS WHERE USUARIO=? AND CONTRASENA=?");
ps.setString(1, USUARIO);
ps.setString(2, CONTRASENA);
ResultSet rs = ps.executeQuery();
rs.next();
if (rs.getString(1).length() > 0 && rs.getString(2).length() > 0) {
resultado = true;
} else {
resultado = false;
}
} catch (SQLException ex) {
resultado = false;
} finally {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(Modelo.class.getName()).log(Level.SEVERE, null, ex);
}
}
return resultado;
}
}
Finally, create a Listener:
import Modelo.Modelo;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
public class Oyente implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/oracle");
Modelo m = new Modelo(ds);
sce.getServletContext().setAttribute("modelo", m);
} catch (NamingException ex) {
Logger.getLogger(Oyente.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}