logo
GBase 8s
适配迁移
文章

创建Maven连接数据库

GBase社区管理员
发表于2024-04-23 09:49:21461次浏览0个评论

1、创建一个新的项目

2、选择Maven

 3、定义组名称

 

4、完成

5、在pom.xml中增加如下内容

 注意:

    右下角会有导入的提示,必须把ifxjdbc.jar放入pjdbc目录下后,再点击导入。

6、导入类

        选择ifxjdbc.jar,右键,add libaries XXX 

7、创建类

      在src/main/java目录下创建包com.pp,在包下面创建类

8、创建查询代码

package com.pp;
import com.pp.util.JdbcUtil;

import java.sql.*;

public class SelJson {
   public static void main( String[] args)
           throws SQLException, ClassNotFoundException {

       try {
           Connection conn=JdbcUtil.getCconnection();
           //查询tabjson数据表里的数据
           String sqls="select * from tabjson where col1=? ";
           PreparedStatement stmt = conn.prepareStatement(sqls);
           stmt.setInt(1,100);
           ResultSet rs1 = stmt.executeQuery();

           while ( rs1.next() ) {
               //输出查询结果
               int col1=rs1.getInt(1);
               String col2=rs1.getString(2);
               System.out.println(col1+"\t"+col2);
           }
           JdbcUtil.CloseResource(conn,stmt,rs1);
       } catch( Exception e) {
           e.printStackTrace();
       }

       return;
   }
}

 

========================使用db.properties====================================

1、新建配置文件

       resources  =>  new  =>  file

 

2、编辑db.properties

driver=com.gbasedbt.jdbc.Driver
url=jdbc:gbasedbt-sqli://192.168.69.219:9022/ttdb:GBASEDBTSERVER=gbase01;NEWCODESET=GB18030,GB18030-2000,5488;DB_LOCALE=zh_cn.GB18030-2000;CLIENT_LOCALE=zh_cn.GB18030-2000;
user=gbasedbt
password=XXXX

 

3、创建JdbcUtil类

package com.pp.util;

import java.sql.*;
import java.util.ResourceBundle;

public class JdbcUtil {
    private static  String driver ;
    private static  String url ;
    private static  String user ;
    private static  String password ;

   static {
       try {
           ResourceBundle bundle = ResourceBundle.getBundle("db");
           driver=bundle.getString("driver");
           url=bundle.getString("url");
           user=bundle.getString("user");
           password=bundle.getString("password");
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   public static Connection getCconnection(){
       try {
           Class.forName(driver);
           Connection connection=DriverManager.getConnection(url,user,password);
           return connection;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }

   public static void CloseConnection(Connection connection){
       if(connection!=null){
           try {
               connection.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
           connection=null;
       }

   }
   public static void CloseStatement(Statement statement){
       if(statement!=null){
           try {
               statement.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
           statement=null;
       }

   }
   public static void CloseResult(ResultSet resultSet){
       if(resultSet!=null){
           try {
               resultSet.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
           resultSet=null;
       }

   }
   public static void CloseResource(Connection connection,Statement statement,ResultSet resultSet){
       CloseResult(resultSet);
       CloseStatement(statement);
       CloseConnection(connection);
   }
}

 

 

 【阅读原文】


 

评论

登录后才可以发表评论