Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie needs advice/guidance re Java web application to query db

Status
Not open for further replies.

mp9

Programmer
Sep 27, 2002
1,379
GB
Hi,

Apologies for such a broad question. I am a Java newbie whose previous Java experience extends to writing "Hello World" type apps and owning a copy of "Java in a Nutshell".

My problem is this. I have been asked to write a web application in Java that will enable users to securely log into a database, perform queries (select, update, delete), and so on.

I don't have a clue where to start. Can anyone suggest a good starting point, or online tutorial for something like this? I've got as far as downloading Eclipse to use as an IDE.

I know this is a very vague post, but all tips/suggestions/recommendations will be gratefully received.

Thanks.

[pc2]
 
You may use Tomcat as the server for JSP, use MySQL as database server and use JDBC to connect to MySQL.
I think you need people to suggest which book can give you a working example. Because of your limited experience, you need to look at book how to configure and how to use jdbc.

Which database will you use?
Which server will you use?
I will suggest you use win2000, winxp pro or winnt as the platform if you are a window user. It is easier to configure application.
 
Windows is not easier to configure Tomcat, Java or MySQL that another OS - this is a fallacy. They are pretty much the same to install whichever OS you are using.

In any case, if you are writing a DMZ webapp, you would be much better off running it on Linux, because of the lesser threat of security holes, attacks and viruses.

Anyway, the OP wasn't asking about OS's so the point is mute.

Tutorials then ...

As prosper said :- Tomcat, JDBC & a database.

Tomcat - this will run/contain your webapp. Download it, read the manual etc :
JDBC - Connect to a database :
Learn JSP/Servlets - what you will write your webapp in :
--------------------------------------------------
Free Database Connection Pooling Software
 
Try this:

Code:
import java.sql.Connection;
import java.sql.DriverManager;

import com.ibm.db.DatabaseConnection;
import com.ibm.db.SQLStatement;
import com.ibm.db.SelectResult;
import com.ibm.db.SelectStatement;
import com.ibm.db.StatementMetaData;

/******************************************************************************
 *
 * AUTHOR: Luken, Michael
 *
 *
 * EXAMPLE OF HOW TO IMPLEMENT TO RETRIEVE A SELECT RESULT OBJECT:
 *
 * SelectResult result = null;
 * LMPPersistenceImpl impl = new LMPPersistenceImpl();
 * try {
 *      impl.setDatasource("MY_DATASOURCE");
 *      impl.setSQL("SELECT * FROM MY_TABLE");
 *      result = impl.query();
 * } catch (Exception e) {
 *      System.out.println(e.toString());
 * } finally {
 *      impl.closeConnections();
 * }
 *
 *
 *
 * EXAMPLE OF HOW TO PERFORM AN UPDATE, INSERT, OR DELETE:
 *
 * LMPPersistenceImpl impl = new LMPPersistenceImpl();
 * try {
 *      impl.setDatasource("MY_DATASOURCE");
 *      impl.setSQL("DELETE FROM MY_TABLE WHERE ID = 5");
 *      impl.execute();
 * } catch (Exception e) {
 *      System.out.println(e.toString());
 * } finally {
 *      impl.closeConnections();
 * }
 *
 ******************************************************************************/

public class LMPPersistenceImpl {

	private String datasourceName = "";
	private String sQL = "";

	private Connection connection = null;
	private DatabaseConnection ibmConnection = null;
	private SelectStatement selectStatement = null;
	private SQLStatement sqlStatement = null;

	public LMPPersistenceImpl() {
		super();
		setDatasource("");
		setSQL("");
	}

	public void closeConnections() {

		try {
			if (ibmConnection != null && ibmConnection.isConnected())
				ibmConnection.disconnect();
			if (connection != null && !connection.isClosed())
				connection.close();
		} catch (Exception e) {
			System.out.println(e.toString());	
		}
	}

	public void execute() throws Exception {

		nullifyConnections();

		sqlStatement = new SQLStatement();
		connection = DriverManager.getConnection("jdbc:odbc:"+getDatasource());
		ibmConnection = new DatabaseConnection(connection);

		sqlStatement.setConnection(ibmConnection);
		sqlStatement.setMetaData(new StatementMetaData());
		
		sqlStatement.getMetaData().setSQL(getSQL());
		sqlStatement.execute();
	}

	private String getDatasource() {
		return datasourceName;
	}

	private String getSQL() {
		return sQL;
	}

	private void nullifyConnections() throws Exception {

		closeConnections();

		connection = null;
		ibmConnection = null;
		selectStatement = null;
		sqlStatement = null;
	}

	public SelectResult query() throws Exception {

		nullifyConnections();

		SelectResult selectResult = null;
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

		selectStatement = new SelectStatement();
		connection = DriverManager.getConnection("jdbc:odbc:"+getDatasource());
		ibmConnection = new DatabaseConnection(connection);

		selectStatement.setConnection(ibmConnection);
		selectStatement.setMetaData(new StatementMetaData());
		selectStatement.getMetaData().setSQL( getSQL() );
		selectStatement.execute();
		selectResult = selectStatement.getResult();

		return selectResult;
	}

	public void setDatasource(String aName) {
		datasourceName = aName;
	}

	public void setSQL(String aValue) {
		sQL = aValue;
	}
}

You will still need to setup your DSN entries and all that... This should be helpful though...
 
Thanks to all for your suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top