在linux上运行Java项目

写作前言:
有一次我发现,没有服务器的root 和数据库的root,用sqlplus查询的乱码问题实在是太难解决了,所以就采用了java的jdbc 的方法进行查询,但是我发现在linux上面运行java 项目带 jar包的那种真的有点麻烦,所以就研究了一下。


目录:

文章目录

    • @[toc]
    • 7.加入新包运行出错的解决办法:重新打包 重新生成 配置文件

正文

#在linux上运行Java项目
##1.用inteliji打包
###1.1注意新建一个resource目录,然后把MANIFEST.MF文件放到这里面,据说如果是默认,会出错
###1.2 选择copy 是为了包之间 不杂糅
在linux上运行Java项目_第1张图片

然后一路默认 最后build artifical

##2.将jar包的根目录上传到linux上,把MANIFEST.MF(里面说明了哪个是main文件,哪个是外来jar) 文件记得拷贝过去,然后运行以下命令

java -jar import_wdgh.jar  这个包中含有main函数

这是拷贝之后的目录结构
在linux上运行Java项目_第2张图片

##3.ok!
在linux上运行Java项目_第3张图片

7.加入新包运行出错的解决办法:重新打包 重新生成 配置文件

在linux上运行Java项目_第4张图片

##6.用到的一些运行的命令
java -jar Import_gh.jar
java -jar Import_gh.jar > userimformation.txt 将输出信息写到文件总,不换行

##5.代码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Formatter;

public class Operator {
    private static String USERNAMR = "ug";
    private static String PASSWORD = "ug";
    public static String DRVIER = "oracle.jdbc.driver.OracleDriver";
    private Formatter f;
    private static String URL = "jdbc:oracle:thin:@//url:1521/orcl";
    Connection connection;
    PreparedStatement pstm;
    Statement stmt;
    ResultSet rs;

    public Operator() {
        this.f = new Formatter(System.out);
        this.connection = null;
        this.pstm = null;
        this.stmt = null;
        this.rs = null;
    }

    public void AddData(String stuName, int gender, int age, String address) {
        this.connection = this.getConnection();
        String sql = "select count(*) from student where 1 = 1";
        String sqlStr = "insert into student values(?,?,?,?,?)";
        int count = 0;

        try {
            this.pstm = this.connection.prepareStatement(sql);
            this.rs = this.pstm.executeQuery();

            while(this.rs.next()) {
                count = this.rs.getInt(1) + 1;
                System.out.println(this.rs.getInt(1));
            }

            this.pstm = this.connection.prepareStatement(sqlStr);
            this.pstm.setInt(1, count);
            this.pstm.setString(2, stuName);
            this.pstm.setInt(3, gender);
            this.pstm.setInt(4, age);
            this.pstm.setString(5, address);
            this.pstm.executeUpdate();
        } catch (SQLException var12) {
            var12.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public void AddData_new(String sql) {
        this.connection = this.getConnection();

        try {
            this.stmt = this.connection.createStatement();
            this.stmt.executeUpdate(sql);
        } catch (SQLException var6) {
            var6.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public void DeleteData(String stuName) {
        this.connection = this.getConnection();
        String sqlStr = "delete from student where stu_name=?";
        System.out.println(stuName);

        try {
            this.pstm = this.connection.prepareStatement(sqlStr);
            this.pstm.setString(1, stuName);
            this.pstm.executeUpdate();
        } catch (SQLException var7) {
            var7.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public void DeleteTable(String name) {
        this.connection = this.getConnection();
        String sqlStr = "DROP TABLE " + name;

        try {
            this.pstm = this.connection.prepareStatement(sqlStr);
            this.pstm.executeUpdate();
        } catch (SQLException var7) {
            var7.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public void UpdateData(String stuName, int gender, int age, String address) {
        this.connection = this.getConnection();
        String sql = "select id from student where 1 = 1 and stu_name = ?";
        String sqlStr = "update student set stu_name=?,gender=?,age=?,address=? where id=?";
        int count = 0;

        try {
            this.pstm = this.connection.prepareStatement(sql);
            this.pstm.setString(1, stuName);
            this.rs = this.pstm.executeQuery();

            while(this.rs.next()) {
                count = this.rs.getInt(1);
                System.out.println(this.rs.getInt(1));
            }

            this.pstm = this.connection.prepareStatement(sqlStr);
            this.pstm.setString(1, stuName);
            this.pstm.setInt(2, gender);
            this.pstm.setInt(3, age);
            this.pstm.setString(4, address);
            this.pstm.setInt(5, count);
            this.pstm.executeUpdate();
        } catch (SQLException var12) {
            var12.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public void SelectData(String sql) {
        String str = "";
        this.connection = this.getConnection();

        try {
            this.pstm = this.connection.prepareStatement(sql);
            this.rs = this.pstm.executeQuery();
            System.out.println(str);
            this.f.format("%-10s %20 %10s %10s %10s %10s %10s\n", "单位编号", "单位全称", "单位简称", "上级单位", "单位级别码", "单位级别", "单位类型");

            while(this.rs.next()) {
                this.f.format("%-10s", this.rs.getString("bh"));
                this.f.format("%-20s", this.rs.getString("dwmc"));
                this.f.format("%-10s", this.rs.getString("dwjc"));
                this.f.format("%-10s", this.rs.getString("sjdwdm"));
                this.f.format("%-10s", this.rs.getString("dwjbm"));
                this.f.format("%-10s", this.rs.getString("dwjb"));
                this.f.format("%-10s \n", this.rs.getString("dwlx"));
            }
        } catch (SQLException var7) {
            var7.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public void SelectData2() {
        this.connection = this.getConnection();
        String sql = "select * from employees where 1 = 1";
        int count = 0;

        try {
            this.pstm = this.connection.prepareStatement(sql);

            for(this.rs = this.pstm.executeQuery(); this.rs.next(); ++count) {
                ;
            }

            ResultSetMetaData rsmd = this.rs.getMetaData();
            int cols_len = rsmd.getColumnCount();
            System.out.println("count=" + count + "\tcols_len=" + cols_len);
        } catch (SQLException var8) {
            var8.printStackTrace();
        } finally {
            this.ReleaseResource();
        }

    }

    public Connection getConnection() {
        try {
            Class.forName(DRVIER);
            this.connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
            System.out.println("成功连接数据库");
        } catch (ClassNotFoundException var2) {
            throw new RuntimeException("class not find !", var2);
        } catch (SQLException var3) {
            throw new RuntimeException("get connection error!", var3);
        }

        return this.connection;
    }

    public void ReleaseResource() {
        if (this.rs != null) {
            try {
                this.rs.close();
            } catch (SQLException var4) {
                var4.printStackTrace();
            }
        }

        if (this.pstm != null) {
            try {
                this.pstm.close();
            } catch (SQLException var3) {
                var3.printStackTrace();
            }
        }

        if (this.connection != null) {
            try {
                this.connection.close();
            } catch (SQLException var2) {
                var2.printStackTrace();
            }
        }

    }
}

你可能感兴趣的:(在linux上运行Java项目)