我使用什么简单的DB解决方案从Eclipse提取我的解析变量到DB [英] What simple DB-solution do I use to extract my parsed variables from eclipse to a DB

查看:146
本文介绍了我使用什么简单的DB解决方案从Eclipse提取我的解析变量到DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一些解析,并将字符串作为结果。
现在我想把它写到一个本地数据库,看看我是否可以通常做一个更大的规模。我选择什么数据库,这是容易处理?第一步是什么?



/ edit。我和eclipse一起工作。我是编程的新手。安装MS SQL Express,但我不知何故不能处理它。不知道从哪里开始...



非常感谢。

解决方案

您可以使用MySQL数据库。



1)下载 mysql连接器并将jar添加到您的项目。



2)在db中创建一个表,




$ b




$ b

数据库{
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 = //您的值;
dbName =您的值
dbtablename = //您的值
username = //您的值
password = //您的值
}

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 = //你的插入查询
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;
}


}

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


I tested some parsing and have the strings as a result. Now I want to write it to a local Database to see if i can generally do that later on a bigger scale. What Database do I choose, that is easy to handle? What are the first steps?

/edit. I work with eclipse. And I'm quite new to programming. Installed MS SQL Express, but I somehow can't handle it. Don't know where to start...

Thank you so much.

解决方案

You can use MySQL Database.

1) Download mysql connector and add jar to your project.

2) Create a table in db and use this table to insert your values there.

java code for database

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


}

EDIT Also you can use MySQL WorkBench to handle your database.

这篇关于我使用什么简单的DB解决方案从Eclipse提取我的解析变量到DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆