java 连接oracle数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class oracleTest {
public static Connection testOracle(){
Connection con =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始连接");
String url="jdbc:oracle:"+"thin:@localhost:1521:orcl";
String user="lhy";
String password="lhy";
con=DriverManager.getConnection(url, user, password);
System.out.println("连接成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void main(String args[]){
Connection conn=oracleTest.testOracle();
PreparedStatement pre=null;
ResultSet result=null;
try {
pre=conn.prepareStatement("select * from student");
result=pre.executeQuery();
while(result.next()){
int id=result.getInt("stu_id");
String name=result.getString("stu_name");
System.out.println("id:"+id+"name:"+name);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}