java中訪問資料庫操作

來源:互聯網
上載者:User
 首先要裝好驅動。不要像我,折騰了好幾個小時的mysql訪問。後面才發現犯了一個最基本的也是最致命的錯誤--沒有裝驅動!想想都覺得汗顏色。
      幾種常見的資料訪問方式如下:
SQLServer:
url:
jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs
driver:com.microsoft.jdbc.sqlserver.SQLServerDriver
Oracle:
url:
jdbc:oracle:oci8:@newer
jdbc:oracle:thin:@10.0.0.200:1521:newer  (??:jdbc:oracle:thin:@[ip]:[port]:[sid])
driver:oracle.jdbc.driver.OracleDriver
mysql
url:
jdbc:mysql://127.0.0.1:3306/mysql
driver:org.gjt.mm.mysql.Driver
     觀察一下很容易發現格式是一樣的。注意,這裡只列出來學校的三個參數,後面例子中有個具體的應用。

    最新的 的mm.mysql驅動可以直接再www.mysql.org上下載,好像是mysql把mm.mysql收編到了官方驅動中了。下載www.mysql.org上的jdbc驅動就可以了。
   給個實現的例子,不是我寫的,不過覺得不錯,有代表性。
  /*
 * Created on 2005-7-15
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.phzhong;
/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBManager {
//使用者名稱
private String user = "";
//密碼
private String password = "";
//主機
private String host = "";
//資料庫名字
private String database = "";
/*
private String
url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";
*/
private String url ="";

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.