WebService, laden eines Sql- Statements aus xml Datei

fenerli23

Erfahrenes Mitglied
Hi,
habe ein WebService soweit erstellt, dass nun per Eingabe des Statementsname der jeweilige Statement ausgeführt werden soll.
Nun habe ich das Problem, dass es nach der Eingabe des Statements z.B "statm1"
auf dem Bildschirm nichts zu erkennen ist ...

Könntet ihr mal auf mein Code drüber schauen und eventuell weiter helfen...

WebService:
Code:
package meinpackage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.*;
import java.io.*;

import oracle.jdbc.driver.OracleDriver;
import oracle.jdbc.pool.OracleDataSource;


public class Select 
{
	public static void setSelect(String Sql)
	{
		String DbDrv = "oracle.jdbc.driver.OracleDriver",
			   DbUrl = "jdbc:oracle:thin:@localhost:1521:xe",
			   Table = "Employees",
			   Usr   = "hr",
			   Pwd   = "system";
			   
		//String help = "";
		
		//wenn keine Angabe über die Datenbank gemacht wird, soll keine Verbindung 
		//zur Datenbank hergestellt werden 
		if (null != DbDrv && 0 < DbDrv.length() &&
			null != DbUrl && 0 < DbUrl.length() &&
			null != Table && 0 < Table.length()	   )
		{
			Connection conn = null;
			Statement  stmt = null;
			SqlStatements statm = null;
			
			//help = Sql;
			try {
				ApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {"config/applicationContext.xml"});
			    //Sql = in.readLine();
			    statm = (SqlStatements)appCtx.getBean(Sql);
			    Sql = statm.getStatement();
								
				
				Class.forName( DbDrv );
				//Verbindung zur Datenbank "Connection"
				conn = DriverManager.getConnection( DbUrl, Usr, Pwd );
				
				//Es wird ein Objekt erstellt, um Sql Statements an die Datanbank zu senden
				stmt = conn.createStatement();
						
				stmt.executeQuery( Sql );
				
				//schließen der Verbindung sowie des Statements ...
				conn.close();
				
				stmt.close();
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		
	}
}

xml datei:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>	

	<bean id="statm1" class="meinpackage.SqlStatements">
		<property name="id" value="1"/>
		<property name="statement" value="select * from employees"/>
	</bean>

	<bean id="statm2" class="meinpackage.SqlStatements">
		<property name="id" value="2"/>
		<property name="statement" value="select * from bank"/>
	</bean>
	
	<bean id="statm3" class="meinpackage.SqlStatements">
		<property name="id" value="3"/>
		<property name="statement" value="select E.EMPLOYEE_ID, E.LAST_NAME, E.FIRST_NAME, B.ZIP, B.KONTO_ID, B.BANK_NAME from EMPLOYEES E, BANK B where E.EMPLOYEE_ID = B.EMPLOYEE_ID and B.Employee_ID between 100 and 105"/>
	</bean>

	<bean id="statm4" class="meinpackage.SqlStatements">
		<property name="id" value="4"/>
		<property name="statement" value="select * from jobs"/>
	</bean>

</beans>

die getter und setter:
Code:
package spring_bean;

import org.springframework.beans.factory.BeanNameAware;

public class SqlStatements implements BeanNameAware {
	
	private String statement = null;
	private long id = 0;
	private String name = null;
	
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	public String getStatement() {
		return statement;
	}
	
	public void setStatement(String statement) {
		this.statement = statement;
	}
	
	public void setBeanName(String name) {
		this.name = name;	
	}
	
	public String getName() {
		return name;
	}
}

und der Client:
Code:
<!doctype html public "-//w3c//dtd html 4.0 transitional// en" >
<%@ taglib uri="/WEB-INF/taglibs-io.tld" prefix="io" %>

<%@ page import="java.sql.*" isThreadSafe="false" %>
<%@ page import="oracle.jdbc.driver.*" isThreadSafe="false" %>

<%
	final String urlLocalhost=
			"http://localhost:8083/WebServiceSpringSelect/services/WebServiceSpringSelect";
	
	String url   = request.getParameter("url");
	String DbDrv = request.getParameter("DbDrv");
	String DbUrl = request.getParameter("DbUrl");
	String Usr   = request.getParameter("Usr");
	String Pwd   = request.getParameter("Pwd");
	String Table = request.getParameter("Table");
	String Sql   = request.getParameter("Sql");
	
	url   = (null != url)   ? url.trim(): urlLocalhost;
	DbDrv = (null != DbDrv) ? DbDrv.trim(): "oracle.jdbc.driver.OracleDriver";
	DbUrl = (null != DbUrl) ? DbUrl.trim(): "jdbc:oracle:thin:@localhost:1521:xe";
	Usr   = (null != Usr)   ? Usr.trim(): "hr";
	Pwd   = (null != Pwd)   ? Pwd.trim(): "system";
	Table = (null != Table) ? Table.trim(): "Employees";
	Sql   = (null != Sql)   ? Sql.trim(): "";
%>

<html> 
<head> <title> Web Service Client Select Database </title></head>
<body>
<h2> Web Service Client Select Database </h2>

<form method="post"><pre>
Endpoint-Url   :<input type="text"     name="url"    value='<%= url %>' size=80>
Db- Treiber    :<input type="text"     name="DbDrv"  value='<%= DbDrv %>' size=80>
Db- Url        :<input type="text"     name="DbUrl"  value='<%= DbUrl %>' size=80>
Nutzer         :<input type="text"     name="Usr"    value='<%= Usr %>' size=80>
Kennwort       :<input type="password" name="Pwd"    value='<%= Pwd %>' size=80>
Tabellenname   :<input type="text"     name="Table"  value='<%= Table %>' size=80>
Sql- Kommando  :<input type="text"     name="Sql"    value='<%= Sql %>' size=80>
               (nach Änderung anderer Parameter muss SQL-Kommando gelöscht werden)<br>
	           <input type="submit"   name="submit" value="Ausgabe">
</pre></form>


<h3><hr>setSelect("<%= Sql %>") -- &gt;
<% out.flush(); %>
<io:soap url="<%= url %>" SOAPAction="" >
	<io:body>
		<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
			<SOAP-ENV:Body>
				<m:setSelect xmlns:m="http://meinpackage">
					<in0><%= Sql %></in0>
				</m:setSelect>
			</SOAP-ENV:Body>
		</SOAP-ENV:Envelope>
	</io:body>
</io:soap>
</hr></h3>


<% 
	if (request.getParameterNames().hasMoreElements() == true
			&& null != DbDrv && 0 < DbDrv.length()
			&& null != DbUrl && 0 < DbUrl.length()            )
	{
		Connection conn = null;
		Statement  stmt = null;
		ResultSet  rs   = null;
		
		try 
		{
			Class.forName( DbDrv );
			conn = DriverManager.getConnection( DbUrl, Usr, Pwd );
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(Sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			int n = rsmd.getColumnCount();
			out.println("<table border=1 cellspacing=0><tr>");
			for (int i=1; i<=n; i++)		// Achtung: Erste Spalte mit 1 statt 0
				out.println("<th>" + rsmd.getColumnName(i) + "</th>");
			while (rs.next())
			{
				out.println( "</tr><tr>" );	// Somit untereinander ausgegeben	
				for(int i=1; i<=n; i++)		// Achtung: Erste Spalte mit 1 statt 0
					out.println("<td>" + rs.getString(i) + "</td>");
			}
			out.println("</tr></table>");
		}
		finally {
			try { if(null != rs)   rs.close(); } catch (Exception ex) {}
			try { if(null != stmt) stmt.close(); } catch (Exception ex) {}
			try { if(null != conn)   conn.close(); } catch (Exception ex) {}
		}
	}
%>

</body>
</html>
 
Zurück