經典JSP資料庫連接(ORACLE、SQL Server、MySQL)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.