Classic JSP database connection (ORACLE, SQL Server, MySQL)

Source: Internet
Author: User
Tags postgresql stmt

1. Connect oracle8/8i/9i Database (thin mode)

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%@ page import= "java.sql.*"%>
<%@ page contenttype= "text/html;charset=gd2312"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<body>

Connection string for Oracle
<%class.forname ("Oracle.jdbc.driver.oracleDriver"). newinstance ();
String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";

ORCL SID SID for your database (the identification number of the Oracle database)
String user= "SA";
String password= "Tiger";

Constructing connection (session, connection) objects
Connection conn=drivermanager.getconnection (Url,user,password);

Constructs a statement (statement) object, passing the carrier of the SQL statement
Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);

SQL statements
String sql= "SELECT * from Test";

The result set is an object returned by the query results in the data, an object that stores the results of the query, and the ability to manipulate the data, possibly completing the update of the data.
ResultSet rs=stmt.executequery (SQL);

%>

<%

To close a database connection

Rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>


2. Connecting to the SQL Server 7.0/2000 database

<%
connection string for SQL Server
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver")
. newinstance ();

String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename= (database name) Pusb";
Set the account password for the database
String user = "sa";
String password = "123456";

Create a Connection object connection
Connection conn = drivermanager.getconnection (URL, user, password);

Constructs a statement (statement) object, passing the carrier of the SQL statement
Statement stmt = conn
. createstatement (Resultset.type_scroll_sensitive,
resultset.concur_updatable);

String sql = "SELECT * from Test";

return result set
ResultSet rs = stmt.executequery (SQL);

%>

<%

Close the Connection object
Rs.close ();
Stmt.close ();
Conn.close ();
%>

Connect to MySQL Database

<%
Link string for MySQL
/* Class.forName ("Com.mysql.jdbc.Driver") is forcing the JVM to load com.mysql.jdbc.Driver this class into memory,
and register it to the DriverManager class, and then find the appropriate driver class based on the URL in Drivermanager.getconnection (url,user,pwd).
Finally call the driver class's Connect (URL, info) to get the connection object. */
Class.forName ("Org.postgresql.Driver"). newinstance ();
String url= "jdbc:postgresql://localhost/database name";

String user= "MyUser";
String password= "123456";

Create a database Connection object
Connection conn = drivermanager.getconnection (URL, user, password);

Constructs a statement (statement) object, passing the carrier of the SQL statement
Statement stmt = conn
. createstatement (Resultset.type_scroll_sensitive,
resultset.concur_updatable);

String sql = "SELECT * from Test";

return result set
ResultSet rs = stmt.executequery (SQL);

%>

<%
Close the Connection object
Rs.close ();
Stmt.close ();
Conn.close ();
%>

Classic JSP database connection (ORACLE, SQL Server, MySQL)

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.