日期:2014-05-16  浏览次数:20406 次

java连接oracle
在lib添加oracle的驱动包ojdbc6.jar;

在src下创建一个ConnectTest类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ConnectTest {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
//服务器地址:210.35.205.109,端口号:1521,数据库实例名字:wjhdb。
private String username="system";
private String pw="123";
private Connection conn=null;
//用户名字和密码是自己建立的。
/*方法一*/
public Connection Conn1()
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn=DriverManager.getConnection(url,username,pw);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}



public static void main(String s[]) throws SQLException
{
Connect obj=new Connect();
Connection con=obj.Conn1();
Statement sm=con.createStatement();
ResultSet rs=sm.executeQuery("select * from student");
while(rs.next()){
System.out.println("学生:"+rs.getString(4)+"在"+rs.getString(3));
}
/*if(con==null)
System.out.print("连接失败");
else
System.out.print("连接成功");*/

}


}