OrientDB之使用JDBC连接数据库

使用工具:IDEA 2018.3和OrientDB 3.1

  1. 下载jdbc驱动

  2. 将jar包导入IDEA中

  3. 预先准备
    控制台开启OrientDB服务,不然的话无法连接到数据库。
    如果配置了OrientDB环境变量的话就可以像下面这样在控制台直接输入server开启服务:

    C:\Users\Dell>server
               .
              .`        `
              ,      `:.
             `,`    ,:`
             .,.   :,,
             .,,  ,,,
        .    .,.:::::  ````:::::::::     :::::::::
        ,`   .::,,,,::.,,,,,,`;;                      .:    ::::::::::    :::    :::
        `,.  ::,,,,,,,:.,,.`  `                       .:    :::      :::  :::     ::
         ,,:,:,,,,,,,,::.   `        `         ``     .:    :::      :::  :::     ::
          ,,:.,,,,,,,,,: `::, ,,   ::,::`   : :,::`  ::::   :::      :::  :::    :::
           ,:,,,,,,,,,,::,:   ,,  :.    :   ::    :   .:    :::      :::  :::::::
            :,,,,,,,,,,:,::   ,,  :      :  :     :   .:    :::      :::  :::::::::
      `     :,,,,,,,,,,:,::,  ,, .::::::::  :     :   .:    :::      :::  :::     ::
      `,...,,:,,,,,,,,,: .:,. ,, ,,         :     :   .:    :::      :::  :::     ::
        .,,,,::,,,,,,,:  `: , ,,  :     `   :     :   .:    :::      :::  :::     ::
          ...,::,,,,::.. `:  .,,  :,    :   :     :   .:    :::::::::::   :::     ::
               ,::::,,,. `:   ,,   :::::    :     :   .:    :::::::::     ::::::::::
               ,,:` `,,.
              ,,,    .,`
             ,,.     `,                                              VELOCE
           ``        `.
    

    ps:没有配置环境变量的话,可以双击bin目录下的server.bat开启服务。

  4. 开始写代码
    步骤和使用JDBC连接MySQL一样,只要稍微改变一点点就可以了。
    完整代码如下:

    import java.sql.*;
    
    public class OrientJdbcTest {
    
        public static void main(String[] args) {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                // 1、注册驱动
                Class.forName("com.orientechnologies.orient.jdbc.OrientJdbcDriver");
    
                // 2、获取连接
                // 其中 demodb 是orientdb自带的示例数据库,在databases目录下
                conn = DriverManager.getConnection("jdbc:orient:remote:localhost/demodb", "admin", "admin");
    
                // 3、获取数据库操作对象
                String query = "select Name, Surname from Profiles order by Name limit 5";
                ps = conn.prepareStatement(query);
                
                // 4、执行查询
                rs = ps.executeQuery();
                
                // 5、处理结果
                while(rs.next()){
                    System.out.println(rs.getString("Name") + " " + rs.getString("Surname"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                
                // 6、释放资源
                if(rs != null){
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    
                if(ps != null){
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    
                if(conn != null){
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    结果如下:

    "C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" "-javaagent:E:\IDEA\IntelliJ IDEA 2018.3.6\lib\idea_rt.jar=1961:E:\IDEA\IntelliJ IDEA 2018.3.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_251\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\rt.jar;D:\学习笔记\OrientDB\orientTest\out\production\orientTest;E:\OrientDB\orientdb-jdbc-3.1.0-all.jar" OrientJdbcTest
    Aaron Cohen
    Aaron Bowers
    Aaron Stewart
    Abbie King
    Adam Carpenter
    七月 13, 2020 8:24:18 上午 com.orientechnologies.common.log.OLogManager log
    信息: Orient Engine is shutting down...
    七月 13, 2020 8:24:18 上午 com.orientechnologies.common.log.OLogManager log
    信息: Clearing byte buffer pool
    七月 13, 2020 8:24:18 上午 com.orientechnologies.common.log.OLogManager log
    信息: OrientDB Engine shutdown complete
    
    Process finished with exit code 0
    

你可能感兴趣的:(OrientDB)