views:

64

answers:

3

I am new to web development, I have to create a web application in Java using GWT that connects to a database and download a file from it. Regardless of the type of database, what does the web application need in order to create a connection with the database? E.g. an API

+2  A: 

Your WebPage (HTML) does not connect to DataBase ever.

Your WebPage use HTTP protocol to connect to an application server, and your application server is the one connecting to database. As you've mentioned, you are using Java on the server side, so, to connect to database you will use JDBC.

Here is a good tutorial on how to start coding in JDBC.

Pablo Santa Cruz
+1  A: 

You need:

  • An API to connect to the database in question. The API must support the specific kind of server you are connecting to (MySQL, Oracle, MSSQL, etc) or support a generic protocol (such as ODBC).

  • Information about the database server, including a hostname or IP of the server, the name of the database, and a username / password of an account on the server. Normally all of this information will be stored in a connection string.

  • You will probably want some Java code examples to get you started.

Also, as others have said, keep in mind that all database connections are made and maintained by the web server itself. Client pages will NOT be able to access the database directly. However, they can make AJAX requests which in turn cause the server to query a database and return a result for the request.

Justin Ethier
+1  A: 

JdbcTemplate or SimpleJdbcTemplate may be the database API that you are looking for. It takes care of all the plumbing code when it comes to opening and closing the database connections appropriately, transaction handling, and a lot more. Although it is part of Spring, you don't have to develop a Spring application in order to use it.

However, for security reasons you should never connect your database to the web directly. What you need to develop is a web service that is accessible to the user. The method does not expose the SQL statements to the enduser and hereby the database is protected from evil user input such as DROP TABLE your_precious_data.

matsev