java连接到后端的说明

java连接到后端的说明

问题描述:

我在Java语言方面是超级新手.现在我要开始使用Java.

I am super newer in java language. Now i am getting started java.

我很想知道如何将Java与后端连接.

I am keen interst know about how to connect java with back end.

为此,我已经安装了oracle 11g速成版和sql developer.

For that I have been installed oracle 11g express edition and sql developer.

首先,我只是在这里感到困惑,这两个工具(oracle 11g速成版和sql developer)是否足以将Java与后端连接.?

First of all i just confused here, is these two tools are (oracle 11g express edition and sql developer.) enough for connect java with back end.?

我正在使用jdk1.8.0_25和Eclipse编辑器.

And i am using jdk1.8.0_25 and eclipse editor.

如果是,则意味着进一步的过程是什么?

If yes, means, what are the further process to achieve?

我已经对php有所了解,我做了一些连接数据库的工作,从数据库中检索了数据.

Already i know little bit about php, i did something connecting database, retrive the data from database..

所以我的目标是,用Java连接后端的最佳方法是什么?

So what is my goal is, what is the best way to connect backend with java?

任何人都可以通过Java专家一步一步解释吗?

Can anyone java expert explain step step by process?

步骤1:下载Oracle JDBC驱动程序

Step 1 : Download Oracle JDBC Drivers

您可以从此处下载Oracle JDBC驱动程序 .选择适合您的数据库版本的版本.在此示例中,由于我连接到Oracle 11g数据库,所以我使用Oracle 11g JDBC驱动程序.对于Oracle 11g,有两个版本可用:ojdbc5.jar(对于JDK 1.5)和ojdbc6.jar(对于JDK 1.6).为您的Java安装使用适当的版本(Oracle现在需要在线注册才能下载驱动程序).我在本教程中使用ojdbc6.jar.

You can download Oracle JDBC drivers from here. Choose the version appropriate for your database version. In this example, I use the Oracle 11g JDBC driver since I connect to Oracle 11g database. There are two versions available for Oracle 11g, ojdbc5.jar (for JDK 1.5) and ojdbc6.jar (for JDK 1.6). Use appropriate version for your Java installation (Oracle now requires online registration for downloading drivers). I use ojdbc6.jar for this tutorial.

第2步:连接到Oracle的Java程序

Step 2 : Java Program to Connect to Oracle

以下Java程序使用Oracle JDBC驱动程序连接到正在运行的Oracle数据库实例.您可以在任何Oracle数据库上使用该程序,因为本示例使用Oracle内置的虚拟表DUAL来获取系统日期. DUAL使我们能够使用普通的SQL查询获取诸如系统日期之类的值.

The following Java program uses Oracle JDBC driver to connect to a running Oracle database instance. You can use this program on any Oracle database as this example uses Oracle’s built-in dummy table DUAL for fetching system date. DUAL enables us to get values such as system date using a normal SQL query.

//示例Java程序-Oracle数据库连接

// Example Java Program - Oracle Database Connectivity

    import java.sql.Connection;

    import java.sql.Date;

    import java.sql.DriverManager;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;



    public class OracleSample {



    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";

    public static final String DBUSER = "system";

    public static final String DBPASS = "manager";



    public static void main(String[] args) throws SQLException {



        // Load Oracle JDBC Driver

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());



        // Connect to Oracle Database

        Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);



        Statement statement = con.createStatement();



        // Execute a SELECT query on Oracle Dummy DUAL Table. Useful for retrieving system values

        // Enables us to retrieve values as if querying from a table

        ResultSet rs = statement.executeQuery("SELECT SYSDATE FROM DUAL");





        if (rs.next()) {

            Date currentDate = rs.getDate(1); // get first column returned

            System.out.println("Current Date from Oracle is : "+currentDate);

      }

        rs.close();

        statement.close();

        con.close();

    }

}

在运行程序之前,请确保更改DBURL,DBUSER和DBPASS的值. DBURL的格式为 jdbc:oracle:thin:@machinename:1521:databasename 将机器名替换为运行oracle的计算机的名称,并将数据库名替换为数据库实例的服务名称. 有关JDBC API的更多详细信息,请参见此页面.

Before you run the program ensure that you change the values for DBURL, DBUSER and DBPASS. DBURL is of the form, jdbc:oracle:thin:@machinename:1521:databasename Replace machinename with the name of the machine where oracle is running and replace databasename with service name of the database instance. See this page for more details on JDBC API.

第3步:将ojdbc.jar添加到Classpath

Step 3 : Add ojdbc.jar to Classpath

为了编译或运行上述程序,您需要将ojdbc.jar添加到程序的类路径中.如果使用的是NetBeans或Eclipse之类的IDE,则可以将ojdbc.jar添加为依赖库,然后NetBeans会自动将其添加到类路径中.

In order to compile or run the above program, you need to add ojdbc.jar to the classpath of your program. If you are using IDE such as NetBeans or Eclipse, you can add ojdbc.jar as a dependent library and NetBeans will automatically add it to classpath.

如果您是通过命令行运行上述程序的,请将ojdbc.jar复制到上述Java程序所在的文件夹中,然后使用以下命令编译该文件(这会将ojdbc.jar添加到classpath中), javac -classpath ./ojdbc6.jar OracleSample.java 使用以下命令运行Java程序(将ojdbc.jar添加到classpath中), java -classpath"./ojdbc6.jar;".甲骨文样本 请注意,在运行OracleSample时,需要JDCB jar文件和类路径中的当前文件夹.

If you are running the above program from command line, copy ojdbc.jar to the folder where the above Java program is located and then compile the file using the following command (this adds ojdbc.jar to classpath), javac -classpath ./ojdbc6.jar OracleSample.java Run the Java program using the following command (ojdbc.jar is added to classpath), java -classpath "./ojdbc6.jar;." OracleSample Note that when you are running OracleSample, you need both the JDCB jar file and the current folder in the classpath.