本帖最后由 赵熠东(赵东东) 于 2013-5-6 21:59 编辑
搞了一天就差“as sysdba”这一句话怎么也连不上Oracle数据库- package jdbc;
- import javax.swing.*;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.sql.*;
- public class DatabaseCon extends JFrame {
- private static final long serialVersionUID = 1L;
- JTextArea taInfo = new JTextArea();
- public DatabaseCon() {
- super("连接数据库");
- setSize(600, 200);
- this.getContentPane().add(taInfo);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void connection() {
- try {
- // 加载JDBC-ODBC桥驱动程序
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.exit(-1);
- }
- try {
- // 试图通过JDBC-ODBC桥建立一个与学生信息管理数据数据源的连接
- String URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
- // String URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";与上面写法效果一样
- Connection con = DriverManager.getConnection(URL, "sys as sysdba",
- "828713"); // 注意sys用户需要加“as sysdba”才能连接数据库
- if (!con.isClosed()) {// 判断数据库连接是否成功
- taInfo.setText("数据库连接成功!\n");
- } else {
- taInfo.setText("数据库连接失败");
- }
- Statement stmt = con.createStatement();
- String sql = "select * from user_col_privs";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- taInfo.append(rs.getString(1) + "\t");
- taInfo.append(rs.getString(2) + "\t");
- taInfo.append(rs.getString(3) + "\t");
- taInfo.append(rs.getString(4) + "\t");
- taInfo.append(rs.getString(5) + "\t");
- taInfo.append(rs.getString(6) + "\t");
- taInfo.append(rs.getString(7) + "\n");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- DatabaseCon frame = new DatabaseCon();
- frame.setVisible(true);
- frame.connection();
- }
- }
复制代码 |
|