JDBC连接Oracle数据库

连接方法:
1. 在MyEclipse中导入Oracle的JDBC驱动包
(oracle\jbdc\lib\classes12.jar);
2.驱动程序:oracle.jdbc.driver.OracleDriver
3.连接地址:jdbc:oracle:thin:@localhost:1521:数据库名

TestJDBC.java:
view sourceprint?
import java.sql.*;




public class TestJDBC {


  
public static void main(String[] args) {
      
Connection conn = null;
       
Statement stmt = null;
      
String sql = null;
      
ResultSet rs = null;
      
try {
          
Class.forName("oracle.jdbc.driver.OracleDriver");
          
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mgc", "system", "admin");
          
stmt = conn.createStatement();
          
sql = "Select * FROM member";
           
rs = stmt.executeQuery(sql);
          
while (rs.next()) {
              
System.out.println(rs.getInt("id") + "," + rs.getString("name"));
           
}
       
} catch (ClassNotFoundException e) {
           
e.printStackTrace();
      
} catch (SQLException e) {
          
e.printStackTrace();
      
} finally {
           
try {
               
if (rs != null) {
                  
rs.close();
                  
rs = null;
               
}
               
if (stmt != null) {
                   
stmt.close();
                  
stmt = null;
              
}
               
if (conn != null) {
                   
conn.close();
                   
conn = null;
               
}
          
} catch (SQLException e) {
               
e.printStackTrace();
           
}
       
}
  
}


}
create.sql:
view sourceprint?
create TABLE member
(
id number not null primary key,
name varchar(5) not null
);
Insert INTO member(id,name) VALUES(1,'magci');
Insert INTO member(id,name) VALUES(2,'mgc');
Insert INTO member(id,name) VALUES(3,'J2EE');
Insert INTO member(id,name) VALUES(4,'JAVA');
Insert INTO member(id,name) VALUES(5,'J2SE');
commit;

你可能感兴趣的:(oracle,sql,jdbc,MyEclipse,J2SE)