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

时间:2014-04-30 10:50:13

标签: java mysql

最近我提出了这样的事情:

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

现在最糟糕的是,即使存在更多条目,我也只能从我的数据库中获取名字。

这下面的部分有问题吗?

SELECT `befehlsname` FROM `eigenebenutzerbefehle` WHERE 1

更新

在阅读了一些答案后,我清除了一些东西,现在它可以工作了。 我改变了这种方式:

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

1 个答案:

答案 0 :(得分:0)

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