JSP database connection (code color separation + note) version

Source: Internet
Author: User
Tags informix sybase sybase database
I. jsp connection to oracle8/8i/9i Database (in thin Mode)
Testoracle. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String result = ""; // query result string

String SQL = "select * from test"; // SQL string

// Connection string. Format: "JDBC: Database driver name: Connection Mode: @ Database Server IP: Port Number: Database Sid"
String url = "JDBC: oracle: thin :@ localhost: 1521: orcl ";
String username = "Scott"; // User Name
String Password = "tiger"; // Password

// Create an Oracle database driver instance
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();

// Obtain the connection to the database
Connection conn = drivermanager. getconnection (URL, username, password );
// Create an execution statement object
Statement stmt = conn. createstatement ();
// Execute the SQL statement and return the result set
Resultset rs = stmt.exe cutequery (SQL );

While (Rs. Next ())
{
Result + = "/N content of the first field:" + Rs. getstring (1) + "<br> ";
}

Rs. Close (); // close the result set.
Stmt. Close (); // close the execution statement object
Conn. Close (); // close the connection to the database
%>

<HTML>
<Body>
<% = Result %>
</Body>
</Html>

Ii. jsp connection to SQL Server7.0/2000 database
Testsqlserver. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String SQL = "select * from test ";

// Connection string, format: "JDBC: Company Name: Database driver name: // Database Server IP: port number; databasename = database name"
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = pubs ";
String username = "Scott ";
String Password = "tiger ";

Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver"). newinstance ();

Connection conn = drivermanager. getconnection (URL, username, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = stmt.exe cutequery (SQL );

%>

<HTML>
<Body>

<%
While (Rs. Next ())
{
%>
The content of the first field is: <% = Rs. getstrisng (1) %> <br>
<%
}

Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>

</Body>
</Html>

Iii. Connecting to the DB2 database using JSP
Testdb2.jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String SQL = "select * from test ";

// Connection string. Format: "JDBC: Database driver name: // Database Server IP: Port Number/database name"
String url = "JDBC: DB2: // localhost: 5000/sample ";
String username = "Scott ";
String Password = "tiger ";

Class. forname ("com. IBM. db2.jdbc. App. db2driver"). newinstance ();

Connection conn = drivermanager. getconnection (URL, username, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = stmt.exe cutequery (SQL );

Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>

4. Connecting to Informix Database Using JSP
Testinformix. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String SQL = "select * from test ";

// Connection string. Format: "JDBC: Database driver name: // Database Server IP: Port Number/Database Name: informixserver = server name; user = user name; Password = password"
String url = "JDBC: Informix-sqli: // 123.45.67.89: 1533/testdb: informixserver = myserver; user = testuser; Password = testpassword ";

Class. forname ("com. Informix. JDBC. ifxdriver"). newinstance ();

Connection conn = drivermanager. getconnection (URL );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = stmt.exe cutequery (SQL );

Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>

V. jsp connection to Sybase Database
Testsybase. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String SQL = "select * from test ";

// Connection string. Format: "JDBC: Company Name: Database driver name: Database Server IP: Port Number/database name"
String url = "JDBC: Sybase: TTL: localhost: 5007/tsdata ";

Properties prop = system. getproperties ();
Prop. Put ("user", "userid"); // User Name
Prop. Put ("password", "user_password"); // Password

Class. forname ("com. Sybase. JDBC. sybdriver"). newinstance ();

Connection conn = drivermanager. getconnection (URL, Prop );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = stmt.exe cutequery (SQL );

Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>

Vi. jsp connection to MySQL database
Testmysql. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String SQL = "select * from test ";

// Connection string. Format: "JDBC: Database driver name: // Database Server IP Address/Database Name? User = username & Password = PASSWORD & use Unicode = Boolean Value & character encoding = encoding"
String url = "JDBC: mysql: // localhost/softforum? User = soft & Password = soft1234 & useunicode = true & characterencoding = 8859_1 ";
String username = "Scott ";
String Password = "tiger ";

Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();

Connection conn = drivermanager. getconnection (URL, username, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = stmt.exe cutequery (SQL );

Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>

VII. jsp connection to PostgreSQL database
Testpostgresql. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
String SQL = "select * from test ";

// Connection string. Format: "JDBC: Database driver name: // Database Server IP Address/database name"
String url = "JDBC: PostgreSQL: // localhost/soft ";
String username = "Scott ";
String Password = "tiger ";

Class. forname ("org. PostgreSQL. Driver"). newinstance ();

Connection conn = drivermanager. getconnection (URL, username, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs = stmt.exe cutequery (SQL );

Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>

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.