1.连接和操作数据库
1.1 java连接数据库两种方式
jdbc直连
使用ojdbc6.jar包进行数据库链接
package com.test1
import.java.sql.*;
class Oracle{
public static void main(String[] args){
Connecton ct=
null;
PreparedStatement ps =
null;
ResultSet rs =
null;
try{
Class.forName(
"oracle.jdbc.driver.OracleDriver");
ct = DriverManager.getConnection(
"jdbc:oracle:thin@127.0.0.1:1521:ORCL",
"scott",
"admin");
ps = ct.prepareStatement(
"select * from emp");
System.
out.print(ps);
rs = ps.executeQuery();
while(rs.next()){
System.
out.println(rs.getString(
"ename"));
}
}
catch(Exception e){
System.
out.println(e.trace);
}
finally{
try{
if(rs!=
null){
rs.close();
rs =
null;
}
if(ps!=
null){
ps.close();
ps =
null;
}
if(ct!=
null){
ct.close();
ct =
null;
}
}
catch(Exception e){
}
}
}
}
jdbc-odbc桥连
1).配置数据源
控制面板,管理工具下配置
2)连接
Class
.forName(
"sun.jdbc.odbc.jdbcOdbcDriver")
Connection ct = DriverManager
.getConnection(
"jdbc:odbc:test",
"username",
"pwd")