¡¡¡¡1.ÔÚd:\oracle\ora92\jdbc\libÏÂ(ÄúµÄoracle°²×°Ä¿Â¼)ÓÐÐí¶à.jar°üÎļþ£¬°ÑÆäÖеÄojdbc14.jar¸´ÖƵ½ÄãµÄÏîÄ¿ÖУ¬È»ºóÓÒ»÷¸ÃÎļþ£¬Ñ¡Ôñ¡°¹¹½¨Â·¾¶¡± Ñ¡ÔñÆäÖеġ°µ¼È롱ѡÏÏÖÔÚ¿ÉÒÔ´ò¿ª²¢Ê¹ÓðüϵÄÀàÎļþÁË¡£µ±È»£¬Ò²¿ÉÒÔ£¬°Ñ¸ÃÇý¶¯³ÌÐò·ÅÔÚCLASSPATH»·¾³±äÁ¿ÖУ¬ÆäËûÀà°üµÄÉèÖÃÒ²ÀàËÆ!
¡¡¡¡2.¼ÓÔؼ°×¢²áÇý¶¯³ÌÐò.
[code]Class.forName("oracle.jdbc.driver.OracleDriver");[/code]
¡¡¡¡¼ÓÔØÇý¶¯³ÌÐòºó£¬Ò»°ã»á½¨Á¢Ò»¸öDriver¶ÔÏ󣬲¢¾Óɵ÷ÓÃDriverManager.registerDriver()À´×Ô¶¯×¢²á´Ë¶ÔÏó¡£
¡¡¡¡3£¬½¨Á¢Á¬½Ó.
[code]DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:lc",name,pass);[/code]
¡¡¡¡ÆäÖÐJDBC url µÄ±ê×¼Óï·¨ÈçÏÂ:
[code]
¡¡¡¡¿ÉÖª£¬Æä·ÖΪÈý¸ö²¿·Ö:
¡¡¡¡protocol:Ö÷ҪͨѶÐÒé
¡¡¡¡subprotocol:´ÎÒªµÄͨѶÐÒ飬ÆäÇý¶¯µÄÃû³Æ
¡¡¡¡data source identifier:Êý¾ÝÀ´Ô´
¡¡¡¡ÈçÉÏÀý×ÓËùʾ:
¡¡¡¡"jdbc:oracle:thin"ÊÇͨѶÐÒ飬@ºóΪ"ÓÐЧµÄÖ÷»úµØÖ·£¬È»ºóÊǶ˿ںÅ,ĬÈϵÄÊÇ:1521.È»ºóÊÇÄãµÄÊý¾ÝÔ´£¬ÏÂÃæÏë±Ø´ó¼Ò¶¼ÖªµÀÁË£¬¾ÍÊÇÓû§ÃûºÍ¿ÚÁî,µ±È»Ò²¿Éд³öÈçÏÂÐÎʽ:
[code]Connection con= DriverManager.getConnection("jdbc:oracle:thin:name/pass@localhost:1521:lc");[/code]
¡¡¡¡·µ»ØÒ»¸öConnection µÄ¶ÔÏócon£¬
¡¡¡¡4.½¨Á¢Ò»¸ösql³ÂÊöʽ¶ÔÏó
¡¡¡¡¸ÃÀý×ÓÊǽ¨Á¢Á˸öÔ¤ÖÆÓï¾ä.
[code]PreparedStatement pstmt =
con.prepareStatement(sqlÓï¾ä);[/code]
¡¡¡¡Ò²¿ÉÓÃ
[code]Statement stmt=con.createStatement();[/code]
¡¡¡¡Æ䲻֮ͬ´¦Çë²é°ïÖúÎĵµ.
¡¡¡¡5,Ö´ÐиÃÓï¾ä
[code]executeUpdate(),executeQuery(),execute(),[/code]
¡¡¡¡Ê¹Ó÷½·¨Çë²é°ïÖúÎĵµ
¡¡¡¡6£¬×îºóÇåÀí¹¤×÷
¡¡¡¡¹Ø±ÕÁ¬½Ó£¬×ö¸öÍêÕûµÄÀý×ÓÈçÏÂ:
¡¡¡¡¸ÃÀàÍê³É¼ÓÔؼ°Á¬½Ó£¬
[code]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
public static Connection dbConn(String name,String pass) {
Connection c = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
c =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:sumoonbest",name,pass);
} catch (SQLException e1) {
e1.printStackTrace();
}
return c;
}
}[/code]
¡¡¡¡¸ÃÀàÍê³É·¢ËÍÓï¾ä£¬Ö´ÐУ¬ÇåÀí
[code]import java.sql.*;
public class DB extends DBConnection {
private static Connection con = null;
public static void main(String[] args) {
try {
con= dbConn("sumoonbest","8848");
if (con == null) {
System.out.print("Á¬½Óʧ°Ü");
System.exit(0);
}
String url = "delete from t_user where username='wang'";
PreparedStatement pres = conn.prepareStatement(url);
System.out.print(pres.executeUpdate()) ;
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}[/code]