Jsp connection to various database code and read data instances

Source: Internet
Author: User
Tags informix sybase sybase database

Many jsp beginners often ask how to connect to the database? Why can't I connect to a database or make statements always fail? Therefore, Chinese Webpage Design writes an article for your reference, mainly including links to the following seven types of databases.

I. jsp connection to MySQL database

Ii. jsp connection to SQL Server7.0/2000 database

Iii. Connecting to the DB2 database using jsp

4. Connecting to Informix Database Using jsp

V. jsp connection to Sybase Database

6. Connecting PostgreSQL database through jsp

VII. jsp connection to Oracle8/8i/9i Database (in thin Mode)

In the following tutorial, we put all the database logic on the jsp page. This is not a good practice, but it is helpful for beginners to learn. 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 statement to create
Create table test (test1 varchar (20), test2 varchar (20)
Write a test record to the table.

I. 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>


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>


6. Connecting PostgreSQL database through jsp

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>
VII. 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>

 

 

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.