Java 连接oracle11g2
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ctl", "scott", "Admin518"); //ctl 为全局数据库名(自己安装数据库时设置的) "scott", "Admin518"分别为用户名和密码
stmt = conn.prepareStatement("select * from emp where deptno=?");
stmt.setInt(1, 10);
rs = stmt.executeQuery();
while (rs.next()) {
System.out.print(rs.getInt("empno"));
System.out.print(rs.getString("ename"));
System.out.print(rs.getString("job"));
System.out.print(rs.getInt("mgr"));
System.out.print(rs.getDate("hiredate"));
System.out.print(rs.getInt("sal"));
System.out.print(rs.getInt("comm"));
System.out.println(rs.getInt("deptno"));
System.out.println("----------------------------");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
if(rs!= null) rs.close();
if(stmt!= null) stmt.close();
if(conn != null) conn.close();
}
}
}