標籤:microsoft 建立 name 驅動 java 資料 local exec w3c
1、串連ORACLE8/8I/9I資料庫(thin模式)
<%@ 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">
<html>
<body>
//oracle的連接字串
<%Class.forName("oracle.jdbc.driver.oracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為你的資料庫的SID sid(Oracle資料庫的標識號)
String user="sa";
String password="tiger";
//構造Connection(會話、串連)對象
Connection conn=DriverManager.getConnection(url,user,password);
//構造Statement(語句)對象,傳遞sql語句的載體
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//SQL 陳述式
String sql="select * from test";
//結果集 是資料中查詢結果返回的一種對象,是一個儲存查詢結果的對象,還具有操縱資料的功能,可能完成對資料的更新。
ResultSet rs=stmt.executeQuery(sql);
%>
<%
//關閉資料庫連接
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
2、串連SQL Server 7.0/2000資料庫
<%
//SQLServer的連接字串
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=(資料庫名字)pusb";
//設定資料庫的帳號密碼
String user = "sa";
String password = "123456";
//建立連線物件Connection
Connection conn = DriverManager.getConnection(url, user, password);
//構造Statement(語句)對象,傳遞sql語句的載體
Statement stmt = conn
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from test";
//返回結果集
ResultSet rs = stmt.executeQuery(sql);
%>
<%
//關閉連線物件
rs.close();
stmt.close();
conn.close();
%>
//串連MySQL資料庫
<%
//mysql的連結字串
/* Class.forName(“com.mysql.jdbc.Driver”)是 強制JVM將com.mysql.jdbc.Driver這個類載入入記憶體,
並將其註冊到DriverManager類,然後根據DriverManager.getConnection(url,user,pwd)中的url找到相應的驅動類,
最後調用該該驅動類的connect(url, info)來獲得connection對象。 */
Class.forName("org.postgresql.Driver").newInstance();
String url="jdbc:postgresql://localhost/資料庫名";
String user="myuser";
String password="123456";
//建立資料庫連接對象
Connection conn = DriverManager.getConnection(url, user, password);
//構造Statement(語句)對象,傳遞sql語句的載體
Statement stmt = conn
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from test";
//返回結果集
ResultSet rs = stmt.executeQuery(sql);
%>
<%
//關閉連線物件
rs.close();
stmt.close();
conn.close();
%>
經典JSP資料庫連接(ORACLE、SQL Server、MySQL)