简体   繁体   English

在数据库中查询表中的所有名称?

[英]Query the database for all names in a table?

Recently I came up with something like this: 最近我想到了这样的东西:

  public static String allCmds() throws Exception {
        try {
              // This will load the MySQL driver, each DB has its own driver
              Class.forName("com.mysql.jdbc.Driver");
              // Setup the connection with the DB
              connect = DriverManager.getConnection(""+MyBot.mysqlDbPath+"",""+MyBot.mysqlDbUsername+"",""+MyBot.mysqlDbPassword+"");
              PreparedStatement zpst=null;
              ResultSet zrs=null;
              zpst=connect.prepareStatement("SELECT `befehlsname` FROM `eigenebenutzerbefehle` WHERE 1 ");
              zrs=zpst.executeQuery();
              if(zrs.next()){
                 return zrs.getString(1);
              }else{
                  return "-empty-";
              }
            }catch (Exception e) {
                  throw e;
                } finally {
                  close();
                }
          }

The bad thing now is, I am only able to get the first name out of my database even when there are more entries present in it. 现在的坏事是,即使数据库中存在更多条目,我也只能从数据库中获取名字。

Is something wrong with below part here? 这下面有什么问题吗?

SELECT `befehlsname` FROM `eigenebenutzerbefehle` WHERE 1

Update 更新资料

After reading some answers I cleared some stuff out and now it works. 阅读一些答案后,我清除了一些内容,现在可以使用了。 I changed it this way: 我这样改变了它:

  public static void allCmds() throws Exception {
        try {
              // This will load the MySQL driver, each DB has its own driver
              Class.forName("com.mysql.jdbc.Driver");
              // Setup the connection with the DB
              connect = DriverManager.getConnection(""+MyBot.mysqlDbPath+"",""+MyBot.mysqlDbUsername+"",""+MyBot.mysqlDbPassword+"");
              PreparedStatement zpst=null;
              ResultSet zrs=null;
              zpst=connect.prepareStatement("SELECT `befehlsname` FROM `eigenebenutzerbefehle`");
              zrs=zpst.executeQuery();

              while (zrs.next()){
                  MyBot.commandlist.add(zrs.getString(1));
              }
            }catch (Exception e) {
                  throw e;
                } finally {
                  close();
                }
          }
package com.example.sql;


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

public class JDBCExample {
    public static Connection con;

    public static void main(String[] argv) {
      try
        {
            connectionQuery();

            PreparedStatement statement =  con.prepareStatement("SELECT `befehlsname` FROM `eigenebenutzerbefehle` WHERE 1 ");
            ResultSet result = statement.executeQuery();
            System.out.println("DataBase table accessed");

            while(result.next())
            {
               String retrievedid= result.getString("befehlsname");
               System.out.println(retrievedid);
            }


            con.close();
        }

        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage().toString());
        }
  }

  public static void connectionQuery()
  {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root","root");
            System.out.println("Remote DB connection established");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
            System.out.println("Remote server could not be connected");
        }
        catch (NullPointerException e)
        {
            e.printStackTrace();
            System.out.println("Remote server could not be connected");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            System.out.println("Remote db connection establishment error");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("False query");
        }
    }
}

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

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