Some common methods of connecting to the database in JSP

Source: Internet
Author: User
Tags informix sybase sybase database

Many new JSP beginners often ask how to connect to the database. Why are there errors? Therefore, I wrote an article here for your reference. In fact, it is not a good practice to put all the database logic in JSP, but it is helpful for beginners to learn. So I did this, when you learn a certain degree, you can consider using the MVC mode for development. When practicing the code, you must put the JDBC driver in the server's class path, and then create a table test in the database. There are two fields: test1, Test2, you can use the following SQL to create

Create Table Test (test1 varchar (20), Test2 varchar (20)

Write a test record to the table.

Now let's start our JSP and database journey.

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. *" %>

<HTML>

<Body>

<% Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();

String url = "JDBC: oracle: thin :@ localhost: 1521: orcl ";

// Orcl is the SID of your database

String user = "Scott ";

String Password = "tiger ";

Connection conn = drivermanager. getconnection (URL, user, password );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</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. *" %>

<HTML>

<Body>

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

String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = pubs ";

// Pubs for your database

String user = "sa ";

String Password = "";

Connection conn = drivermanager. getconnection (URL, user, password );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% 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. *" %>

<HTML>

<Body>

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

String url = "JDBC: DB2: // localhost: 5000/sample ";

// Sample is your database name

String user = "admin ";

String Password = "";

Connection conn = drivermanager. getconnection (URL, user, password );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</Body>

</Html>

4. Connecting to Informix Database Using JSP

Testinformix. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. SQL. *" %>

<HTML>

<Body>

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

String url =

"JDBC: Informix-sqli: // 123.45.67.89: 1533/testdb: informixserver = myserver;

User = testuser; Password = testpassword ";

// Testdb is your database name

Connection conn = drivermanager. getconnection (URL );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</Body>

</Html>

V. jsp connection to Sybase Database

Testmysql. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. SQL. *" %>

<HTML>

<Body>

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

String url = "JDBC: Sybase: TTL: localhost: 5007/tsdata ";

// Tsdata is your database name

Properties sysprops = system. getproperties ();

Sysprops. Put ("user", "userid ");

Sysprops. Put ("password", "user_password ");

Connection conn = drivermanager. getconnection (URL, sysprops );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</Body>

</Html>

Vi. jsp connection to MySQL database

Testmysql. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. SQL. *" %>

<HTML>

<Body>

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

String url = "JDBC: mysql: // localhost/softforum? User = soft & Password = soft1234 & useunicode = true & characterencoding = 8859_1"

// Testdb is your database name

Connection conn = drivermanager. getconnection (URL );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</Body>

</Html>

VII. jsp connection to PostgreSQL database

Testmysql. jsp is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. SQL. *" %>

<HTML>

<Body>

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

String url = "JDBC: PostgreSQL: // localhost/soft"

// Soft is your database name

String user = "myuser ";

String Password = "mypassword ";

Connection conn = drivermanager. getconnection (URL, user, password );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );

String SQL = "select * from test ";

Resultset rs1_stmt.exe cutequery (SQL );

While (Rs. Next () {%>

The content of your first field is: <% = Rs. getstring (1) %>

Your second field content is: <% = Rs. getstring (2) %>

<% }%>

<% Out. Print ("database operation successful, congratulations"); %>

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</Body>

</Html>

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.