小编典典

我使用哪种简单的数据库解决方案将解析的变量从eclipse提取到数据库

java

我测试了一些解析,并得到了字符串。现在,我想将其写入本地数据库,以查看以后是否可以在更大范围内进行操作。我选择哪种数据库容易处理?第一步是什么?

/编辑。我用日食工作。而且我对编程还很陌生。安装了MS SQL Express,但是我不知怎么处理。不知道从哪里开始…

非常感谢。


阅读 197

收藏
2020-11-30

共1个答案

小编典典

您可以使用MySQL数据库。

1)下载mysql连接器,然后将jar添加到您的项目中。

2)在db中创建一个表,并使用该表在其中插入值。

数据库的Java代码

public class Database {
    private Connection connection = null;
    private Statement statement = null;
    private String serverName = null;
    private String dbName = null;
    private String  dbtablename = null;
    private String username = null;
    private String password = null;

   public Database() {
        serverName = //your value;
        dbName = your value
        dbtablename = //your value
        username = //your value
        password = //your value
    }

   public Connection OpenConnectionDB() {
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            this.connection = DriverManager.getConnection("jdbc:mysql://" + serverName + "/" + dbName + "?useUnicode=yes&characterEncoding=UTF-8&" + "user=" + username + "&password=" + password);
            System.out.println("conection: " + connection);
            //            this.connection = DriverManager.getConnection("jdbc:mysql://" + serverName + "/" + dbName  +"?user=" + username + "&password=" + password );
            //System.out.println("debug: Connect to dbServer");

        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return connection;
    }

    public void closeConnectionDB() {
        if (this.connection != null) {
            try {
                connection.close();
                connection = null;
                //System.out.println("debug: Close dbServer");
            } catch (SQLException ex) {
                System.out.println("debug: Closing Database... FAILED (NOT RUNNING)");
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void insert(){
         String query = //your insert query
         OpenConnectionDB();
         try {
            this.statement = this.connection.createStatement();
                this.statement.execute(query);
                statement.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnectionDB();
    }

   public String getResults(){
        String query = //select query
        OpenConnectionDB();
        try {
            this.statement = this.connection.createStatement();
            ResultSet  rs = this.statement.executeQuery(query);

            if(rs!=null){
                while(rs.next()){
                    return rs.getString("Your_field_name_in_db"); //get your 
                }            
            }

             statement.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        closeConnectionDB();
        return null;
    }


}

编辑 您也可以使用MySQL
WorkBench
处理您的数据库。

2020-11-30