栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

创建Maven连接数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建Maven连接数据库

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);
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号