App下載
首頁(yè)javaconnectionJava JDBC - 如何連接Oracle XE

Java JDBC - 如何連接Oracle XE

我們想知道如何連接Oracle XE。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Main {
  public static void main(String args[]) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    String userName = "suru";
    String password = "password";
    Connection con = DriverManager.getConnection(url, userName, password);
    System.out.println("Connection success!");
    Statement stmt = con.createStatement();
    String sql = "CREATE TABLE EMP ( ID NUMBER(5) PRIMARY KEY, NAME VARCHAR2(50))";
    stmt.execute(sql);
    System.out.println("Table created successfully!");
    stmt.close();
    con.close();
  }
}