Connect to the database instance using Java [reprint]

Source: Internet
Author: User
Tags informix sybase sybase database
The code in this article mainly lists the key code used to connect to the database. Other database access code is omitted.

1. oracle8/8i/9i Database (thin Mode)

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

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

// Orcl is the SID of the database

String user = "test ";

String Password = "test ";

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

2. DB2 database

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 );

3. SQL Server7.0/2000 database

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

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

// Mydb is a database

String user = "sa ";

String Password = "";

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

4. Sybase Database

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

String url = "JDBC: Sybase: TDS: localhost: 5007/mydb ";

// Mydb is your database name

Properties sysprops = system. getproperties ();

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

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

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

5. Informix Database

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

String url =

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

User = testuser; Password = testpassword ";

// Mydb indicates the Database Name

Connection conn = drivermanager. getconnection (URL );

6. MySQL database

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

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

// Mydb indicates the Database Name

Connection conn = drivermanager. getconnection (URL );

7. PostgreSQL database

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

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

// Mydb indicates the Database Name

String user = "myuser ";

String Password = "mypassword ";

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

Example:

1. Access <% @ page contenttype = "text/html; charset = gb2312" Import = "Java. SQL

. * "%>

<% @ Page import = "Java. util. *, java. Text. *" %>

<HTML>

<Title> retrieve and display information in the table </title>

<Body>

<%

Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");

Connection con = drivermanager. getconnection ("JDBC: ODBC: webdata ");

Statement SMT = con. createstatement ();

String SQL;

SQL = "select BH, shrxx, shrdh, shrdz, hwjs, bzxs, YF, BJD from hy ";

Resultset rs = smt.exe cutequery (SQL );

Out. println ("retrieve all information in the table ");

%>

Click Here </font> <a href = "javascript: history. Back (1)"> <B> <font color = "# ff0000"

> Back </font> </B> </a> <HR>

<%

Out. println ("<Table border = '1'> ");

Out. println ("<tr bgcolor = 'yellow'> <TH> bill of lading number </Th> <TH> name of the consignee </Th> <TH> receipt

Person phone number </Th> <TH> consignee's address </Th> <TH> Number of parcels </Th> <TH> packing form </Th> <TH> freight </Th> <

Th> quote </Th> </tr> ");

While (Rs. Next ())

Out. println ("<tr> <TD>" + Rs. getstring (1) + "</TD> <TD>" + Rs. getstring (2) + "</

TD> <TD> "+ Rs. getstring (3) + "</TD> <TD>" + Rs. getstring (4) + "</TD> <TD>" + Rs.

Getstring (5) + "</TD> <TD>" + Rs. getstring (6) + "</TD> <TD>" + Rs. getstring (7) +

"</TD> <TD>" + Rs. getstring (8) + "</TD> </tr> ");

Con. Close ();

%>

<HTML>

<Body>

</Table>

</Body>

</Html>

2. MySQL

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

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

<HTML>

<Body>

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

String url = "JDBC: mysql: /// 192.168.0.1: 3306/test? User = root & Password = 123 ";

Connection conn = drivermanager. getconnection (URL );

Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset.

Concur_updatable );

String SQL = "select * From huahe_article ";

Resultset rs1_stmt.exe cutequery (SQL); %>

<Table> <tr> <TD> the content of your first field is: </TD>

<TD> the content of your second field is: </TD> </tr>

<%

While (Rs. Next () {%>

<Tr> <TD>

<% = Rs. getstring (1) %> </TD> <TD>

<% = Rs. getstring (2) %> </TD> </tr>

<% }%>

</Table>

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

<% Rs. Close ();

Stmt. Close ();

Conn. Close ();

%>

</Body>

3. With MSSQL

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

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

<%!

Public static string getgbstring (string SRC ){

Try {

Return new string (SRC. getbytes ("ISO-8859-1"), "gb2312 ");

} Catch (Java. Io. unsupportedencodingexception e ){

Return NULL;

}

}

%>

<HTML>

<Body>

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

String url = "JDBC: Microsoft: sqlserver: // 192.168.0.1: 1433; databasename = test ";

// Test 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: <% = getgbstring (Rs. getstring (1) %> <br>

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

<% }%>

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

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