简体   繁体   English

如何在web-app中运行这个.java文件作为独奏java应用程序来测试它?

[英]How to run this .java file in a web-app as a solo java application just to test it?

package database;


    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import database.Dbconnect;

    public class CreateQuery {
        Connection conn;

        /**
         * @throws ClassNotFoundException
         * @throws SQLException
         * @throws IOException
         */
        public CreateQuery() throws ClassNotFoundException, SQLException, IOException {
            conn=new Dbconnect().returnDatabaseConnection();
        }
        public int addNewLayertoDB(String feature_name,String shape,int Latitude , int Longitude , int feature_geom , String feature_details){
            try {
                PreparedStatement statement = null;
                String table_name = feature_name + "_" + shape; 
                String query = "CREATE TABLE EtherMap "+table_name+" ("+ feature_name+" (20))";
                statement = conn.prepareStatement(query);
                statement.setString(1, feature_name);
                statement.execute();
                String squery = "ALTER TABLE EtherMap" +table_name+"  ADD COLUMN geom int , ADD COLUMN shape character(10)";
                return 1;
            } catch (SQLException ex) {
                return 0;
            }
        }




        public void closeConn() throws SQLException {
            if (conn != null) {
                this.conn.close();
            }
        }

    }

I want to test this java code to see if anything is being updated in the postgresql database . 我想测试这个java代码,看看postgresql数据库中是否有任何更新。

How do I do this in ecclipse IDE ? 我如何在ecclipse IDE中执行此操作?

There is nothing in this code that requires it to be in a web-app. 此代码中没有任何内容要求它在Web应用程序中。

That's a good thing, as web-apps are components of their Servlet containers. 这是一件好事,因为web-apps是其Servlet容器的组件。 In other words, you can't really run a standalone web-app, you must deploy it. 换句话说,您无法真正运行独立的Web应用程序,您必须部署它。 Perhaps you'll deploy it in a micro-container that only contains your application, but there's nothing like true stand-alone running of a component. 也许您将它部署在仅包含您的应用程序的微容器中,但没有什么比组件的真正独立运行更好。

In your case, just add a public static void main(String[] args) { to this code, put the desired calls to create the class and perform the operations, and give it a spin. 在您的情况下,只需向此代码添加一个public static void main(String[] args) { ,放置所需的调用以创建类并执行操作,并给它一个旋转。 If it's not just a "quick check" but a formal test that might be repeated, look into JUnit. 如果它不只是一个“快速检查”,而是一个可能重复的正式测试,请查看JUnit。

我建议编写一个TestCase并确保(断言)你在postgres中存储的数据,你可以阅读它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM