klasse til databaseforbindelse
Hej Eksperter,Jeg har en klasse som ser således ud
package util;
import java.util.Vector;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class Dbp
{
private Connection conn ;
private PreparedStatement pstmt;
public Dbp()
{
try
{
Context init = new InitialContext();
Context ctx = (Context) init.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/MIN_DS");
Connection conn = ds.getConnection();
}
catch(Exception s)
{
System.out.println("SQL dataforbindelse fejlede"+s);
}
}
public ResultSet select(String sql, Vector params)
{
ResultSet r = null;
try
{
PreparedStatement pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
for (int i=0; i < params.size(); i++) {
pstmt.setString(i+1, (String)params.elementAt(i));
}
r = pstmt.executeQuery();
}
catch(SQLException s)
{
System.out.println("Select fejlede: "+s);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return r;
}
public void insertUpdate(String sql, Vector params)
{
try
{
PreparedStatement pstmt = conn.prepareStatement(sql);
for (int i=0; i < params.size(); i++) {
pstmt.setString(i+1, (String)params.elementAt(i));
}
pstmt.executeUpdate();
}
catch(SQLException s)
{
System.out.println("update/insert fejlede");
}
}
}
Den compiler fint, men den virker ikke.
Jeg vil gerne kunne kalde den fra min jsp side således
<%
Dbp d = new Dbp();
try {
Vector vi = new Vector();
ResultSet rs = d.select("SELECT * from users", vi);
while(rs.next()){
out.print(rs.getString(2));
out.print(rs.getString(3));
}
} catch (SQLException s) {
System.out.println("<h3>Start.jsp SQL fejl: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState() + "</h3>");
}
%>