如何从字段中获取字符串

时间:2016-09-16 14:04:10

标签: java mysql

我在java中有一个mysql问题。我有一个包含不同表的mysql数据库。我目前有一个名为'litebans'的数据库和一个名为'litebans_mutes'的表。

在该表中有一行称为reason,根据这个原因(让我们说理由是什么),有一个名为'This is a test'和'sorry'的字符串;我如何获得字符串'This is a test'和'sorry'与java中相同的'uuid'行相关联?这是一张解释更多的图片:

Here is an image explaining the sql format

此外,我目前已初始化所有变量,例如java,我目前有这样的代码: http://hastebin.com/odumaqazok.java(主要类;将其用于Minecraft插件)

以下代码是MySQL类; api用于连接和执行东西。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.octopusmc.punish.Core;

public class MySQL {

    public static Connection openConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e1) {
            System.err.println(e1);
            e1.printStackTrace();
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://" + Core.host + ":" + Core.port + "/" + Core.database, Core.user, Core.pass);
            System.out.println("Currently connected to the database.");
            return conn;
        } catch (SQLException e) {
            System.out.println("An error has occured while connecting to the database");
            System.err.println(e);
            e.printStackTrace();
        }
        return null;
    }

    public static void Update(String qry) {
        try {
            Statement stmt = Core.SQLConn.createStatement();
            stmt.executeUpdate(qry);

            stmt.close();
        } catch (Exception ex) {
            openConnection();
            System.err.println(ex);
        }
    }

    public static Connection getConnection() {
        return Core.SQLConn;
    }

    public static ResultSet Query(String qry) {
        ResultSet rs = null;
        try {
            Statement stmt = Core.SQLConn.createStatement();
            rs = stmt.executeQuery(qry);
        } catch (Exception ex) {
            openConnection();
            System.err.println(ex);
        }
        return rs;
    }
}

使用上述api的示例如下所示:

try {
    ResultSet rs = MySQL.Query("QUERY GOES HERE");
    while (rs.next()) {
        //do stuff
    }
} catch (Exception err) {
    System.err.println(err);
    err.printStackTrace();
}

tl; dr:我想通过给'uuid'字符串字段获取名为'reason'的两个字段。

2 个答案:

答案 0 :(得分:0)

您应该创建与数据库的连接,然后使用Select Query,您可以获得此值,这是一个可以帮助您的示例,

您需要做的只是更改真实属性和表格的名称

String value;
try {
    Connection connection = con.getConnection();
    Statement stm = connection.createStatement();
    String requete = "SELECT myAttribute FROM myTable";

    ResultSet resultat = stm.executeQuery(requete);

    while (resultat.next()) {
        value = resultat.getString("myAttribute");
    }
} catch (Exception e) {
}

答案 1 :(得分:0)

首先,确保您使用jdbc mysql驱动程序连接到数据库

定义一个类,您可以在其中编写所需的连接并创建语句代码。

例如

class ConnectorAndSQLStatement {

    ResultSet rs = null;
    public Statement st = null;
    public Connection conn = null;

    public connect() {

        try {
            final String driver = "com.mysql.jdbc.Driver";
            final String db_url = "jdbc:mysql://localhost:3306/your_db_name";

            Class.forName(driver);//Loading jdbc Driver
            conn = DriverManager.getConnection(db_url, "username", "password");
            st = conn.createStatement();
            rs = st.executeQuery("Select what_you_want from your_table_name");
            while (rs.next()) {
                String whatever = rs.getInt("whatever ");
                System.out.print(whatever);
            }
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
}

只需调用此函数即可:D 希望它有用