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)