如何在Java中将SYS连接到Oracle?

时间:2012-04-11 07:15:47

标签: java database oracle sys

我收到此错误:

java.sql.SQLException: ORA-28009: connection as SYS should be as SYSDBA or SYSOPER

如何解决? (我需要SYS)。 感谢。

9 个答案:

答案 0 :(得分:36)

试试这个:

import java.sql as jsql
import java.lang as lang
driver, url, user, passwd = (
"oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@localhost:1234:xxx1",
"sys as sysdba",
"xxx1")
 lang.Class.forName(driver)
 c = jsql.DriverManager.getConnection(url,user,passwd)

答案 1 :(得分:8)

答案已经存在,

您尝试以sys身份进行连接,但服务器允许

任一     sys as sysdba

sys as sysoper

只需将用户参数更改为上面的任何一个

user='sys as sysdba'

user='sys as sysoper'

答案 2 :(得分:7)

此代码有效

String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName).newInstance();
String nameForConnect = "sys as sysdba";
String pass = "password";
String url = "jdbc:oracle:thin:@192.168.0.1:1521:ORCL";
Connection conn = DriverManager.getConnection(url, nameForConnect, pass);

答案 3 :(得分:3)

如果您尝试连接到数据库,请执行以下操作:connect SYS/<password>您使用的语法不再有效(在Oracle 9i之后)。

而是尝试按以下方式连接:

connect SYS/<password> as SYSDBA or connect SYS/<password> as SYSOPER

答案 4 :(得分:1)

您是否可以使用OracleDataSource对象?

public class Database {    
    static OracleDataSource ods;    
    public static Connection openConnection(String URL, String user, String password,     String option) throws SQLException
    {
            Connection conn = null;
            Properties properties = new Properties();
            properties.put("user", user);
            properties.put("password", password);

            ods = new OracleDataSource();
            ods.setURL(URL);

            if(option != null)
            {
                properties.put("internal_logon", option);
            }

            ods.setConnectionProperties(properties);
            conn = ods.getConnection();

            return conn;
    }
}

并称之为:

Connection con = null;    
con = Database.openConnection("YourJDBCConnectionURL", "YourSYSUser", "YourSYSPassword", "sysdba");

答案 5 :(得分:1)

如果要将数据库与&#34; sys&#34;以外的用户连接。 as&#34; sysdba&#34;然后你必须改变#34;瘦&#34;到&#34; oci&#34;成功连接。

try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String DB_URL="jdbc:oracle:oci:@localhost:1521:orcl";
        OracleDataSource ds1=new OracleDataSource();
        Properties prop1 = new Properties();
        prop1.setProperty("user","ravi");
        prop1.setProperty("password","******");
        prop1.setProperty("internal_logon","sysdba");
        ds1.setConnectionProperties(prop1);
        ds1.setURL(DB_URL);
        OracleConnection conn1 = (OracleConnection)ds1.getConnection();
        Statement stmt = conn1.createStatement();
        ResultSet rs = stmt.executeQuery("select * from dba_users");
        while (rs.next())
            System.out.println(rs.getString(1));
        conn1.close();
    } catch (Exception e) {
        System.out.println(e);
    }

答案 6 :(得分:1)

        /*It works for me*/
        /*also oci and thin is important if you want to connect with database which is installed in your clien (Computer) add oci if you want to install to server add thin*/

        String dbURL2 = "jdbc:oracle:oci:@172.16.24.123:1521:XE";
        String username = "sys as sysdba";
        String password = "XX Change it to your system password";
        
        try {
        Connection connection = DriverManager.getConnection(dbURL2, username, password);
            System.out.println("Connected to Oracle data");
        
    } catch (SQLException e) {
            System.out.println("Opps ! error");
            e.printStackTrace();
        
    }

答案 7 :(得分:0)

您需要将sysdba与用户字符串参数(如

)放在一起
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#006064"
    android:layout_height="match_parent">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:id="@+id/ll"
        android:weightSum="1"
        android:layout_marginTop="-10dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:clipToPadding="false"
            android:onClick="goToChild"
            android:gravity="center"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:onClick="goToDash"
                android:foreground="?android:attr/selectableItemBackground">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="181dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="70dp"
                        android:layout_weight="1.02"
                        android:background="#B2FF59"
                        android:padding="10dp"
                        android:src="@drawable/dash2"
                        android:onClick="goToChild"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="DASHBOARD"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_margin="10dp"
                        android:background="@color/lightgray" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Open the Dashboard"
                        android:textColor="@android:color/darker_gray"
                        android:textSize="14sp" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:onClick="goToSpy"
                android:foreground="?android:attr/selectableItemBackground">

                <LinearLayout
                    android:id="@+id/routeOption"
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="64dp"
                        android:layout_weight="1.07"
                        android:background="#448AFF"
                        android:padding="10dp"
                        android:src="@drawable/docico" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="MY DOCUMENTS"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_margin="10dp"
                        android:background="@color/lightgray" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Save documents"
                        android:textColor="@android:color/darker_gray"
                        android:textSize="14sp" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:onClick="goToPlanner"
                android:foreground="?android:attr/selectableItemBackground">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:id="@+id/plannerOption"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <ImageView
                        android:id="@+id/imageView5"
                        android:layout_width="207dp"
                        android:layout_height="70dp"
                        android:layout_weight="1.02"
                        android:background="#F44336"
                        android:padding="10dp"
                        android:src="@drawable/plannerico" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="PLANNER"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_margin="10dp"
                        android:background="@color/lightgray" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Set your Plans!"
                        android:layout_marginBottom="1dp"
                        android:textColor="@android:color/darker_gray"
                        android:textSize="14sp" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:onClick="goToPlanner"
                android:foreground="?android:attr/selectableItemBackground">

                <LinearLayout
                    android:id="@+id/taskOption"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="207dp"
                        android:layout_height="57dp"
                        android:layout_weight="1.02"
                        android:background="#FFEB3B"
                        android:padding="10dp"
                        android:src="@drawable/tasks2" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="TASKS"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_margin="10dp"
                        android:background="@color/lightgray" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Tasks by Company"
                        android:layout_marginBottom="1dp"
                        android:textColor="@android:color/darker_gray"
                        android:textSize="14sp" />

                </LinearLayout>
            </android.support.v7.widget.CardView>



        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:clipToPadding="false"
            android:onClick="goToSummary"
            android:gravity="center"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:onClick="goToSummary"
                android:foreground="?android:attr/selectableItemBackground">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="181dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="64dp"
                        android:layout_weight="1.02"
                        android:background="#FFEB3B"
                        android:padding="10dp"
                        android:src="@drawable/remico"
                        android:onClick="goToChild"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="REMARKS"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_margin="10dp"
                        android:background="@color/lightgray" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Company's Remarks"
                        android:textColor="@android:color/darker_gray"
                        android:textSize="14sp" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:onClick="goToSettings"
                android:foreground="?android:attr/selectableItemBackground">

                <LinearLayout
                    android:id="@+id/setting"
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <ImageView
                        android:id="@+id/imageView2"
                        android:layout_width="match_parent"
                        android:layout_height="64dp"
                        android:layout_weight="1.07"
                        android:background="#B2FF59"
                        android:padding="10dp"
                        android:src="@drawable/meetico" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="MEETING MINUTES"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_margin="10dp"
                        android:background="@color/lightgray" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Meetings Summary"
                        android:textColor="@android:color/darker_gray"
                        android:textSize="14sp" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

答案 8 :(得分:0)

我的两分钱遇到了同样的例外。以下对我有用:

LEFT JOIN