JSP direct access database Code does not require ODBC Data sources

Source: Internet
Author: User

Many netizens encountered the following problems when learning JSP programming:
How to economically build a JSP website
What database does the website use? I am not familiar with MySQL. I want to use SQL Server. The space of the service provider is not supported or the price is too high.

, Build a server -- more practical, using access, database connection is a problem, service providers do not provide JDBC-ODBC ......
In short, it is because the database has greatly restricted the practical use of JSP. Does JSP become the spring snow, not here I use my JSP virtual master

The following describes how to build a JSP website by directly connecting to access.

1. First, we need to find a space that supports JSP and access. No !!! Many of them are more expensive than ASP ......
2. log on to the background using FTP and check the directory structure. Take my http://www.itstudy.cn/as an example, as shown below:

3. Create the "www" and "www/Database" directories and JSP files in the website space,

4. Here I create the ACCESS database data. mdb, table tsiteconfig, field fsitename, fsitetitle..., and save the file.

Name Data. mdb to www/database, for example:

5. paste the following code into the index. jsp file:

<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" %>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<%
/*************************************** *********************
* ******** Directly connects to the ACCESS database without the need to set a data source
* ******** Date: 2006.5
* ******** Author:
* ******** If you have any questions, please contact www.itstudy.cn.
**************************************** ********************/

// ******************* Start the database connection code *****************
// Exception Handling statement
Try
{
// Modify the following items
String Spath = "www // database // data. mdb"; // note the path from the website root to the file
String dbpath = application. getrealpath (Spath); // convert it to a physical path
String dbname = ""; // Acess database username, empty if no
String user = ""; // the password of the Acess database. It is null if no password is provided.
// Database connection string
String url = "JDBC: ODBC: Driver = {Microsoft Access Driver (*. mdb)}; DBQ =" + dbpath;
// Load the driver
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
// Establish a connection
Connection conn = drivermanager. getconnection (URL );
// Create a statement object
Statement stmt = conn. createstatement
(Resultset. type_scroll_sensitive, resultset. concur_updatable );
// ***** Database connection code ended *********

*********************** ********
// Create a tsiteconfig table in the database. Two fields UID and PWD are created in the table, whose types are text type.
String SQL = "select * From tsiteconfig ";
Resultset rs = stmt.exe cutequery (SQL );
Out. Print ("<br> congratulations! Database Connection successful! ");
While (Rs. Next ())
{
Out. Print ("username:" + Rs. getint ("ID "));
Out. Print ("Password:" + Rs. getstring ("name") + "<br> ");
}
*********************** ********
Rs. Close (); // close the record set object
Stmt. Close (); // close the statement object
Conn. Close (); // close the connection object

} Catch (exception e ){
Out. Print ("database connection Error !, Error message: <br> ");
Out. Print (E. getmessage ());
}
%>

6. After uploading the file to the server, enter: http://www.itstudy.cn/www1/index.jsp in IE browser
After the page is displayed, the ACCESS database is successfully connected.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.