java - sql 连接错误 - 阻止我从 mysql 获取 'SELECT' 数据

标签 java android mysql sql

我现在尝试了 12 个小时从我的数据库中选择数据,但我做不到。

插入工作正常。只是 SELECT 不起作用。

当我运行 connect() 函数时,我收到很多错误:

      public void connect() {
        String dbUrl = "jdbc:mysql://*****.heliohost.org:3306/*****";
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(dbUrl,"*****","****");
            return;//the debugger never succeed to reach this line

        } catch (Exception e) {
            e.printStackTrace();

        }
      }

我从上面的 connect() 函数得到的错误:

12-23 16:22:54.460: W/dalvikvm(7473): VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
12-23 16:22:54.470: I/dalvikvm(7473): Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom
12-23 16:22:54.470: W/dalvikvm(7473): VFY: unable to resolve virtual method 14440: Ljavax/naming/Reference;.get (Ljava/lang/String;)Ljavax/naming/RefAddr;
12-23 16:22:54.470: D/dalvikvm(7473): VFY: replacing opcode 0x6e at 0x0004
12-23 16:22:54.491: W/dalvikvm(7473): VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
12-23 16:22:54.491: E/dalvikvm(7473): Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
12-23 16:22:54.491: W/dalvikvm(7473): VFY: unable to resolve new-instance 1365 (Ljavax/naming/StringRefAddr;) in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;
12-23 16:22:54.491: D/dalvikvm(7473): VFY: replacing opcode 0x22 at 0x0006

下一个函数仅在到达以下行时才会下降:“String tem = result.getString("name")":

public List<String> selectCategories()
{
    List<String> listCS = new ArrayList<String>();
    try
    {
           Statement statement = connection.createStatement();
           statement.execute("select name from categories");
            ResultSet result=statement.getResultSet();

            if(result!=null)
            {
                while(result.next());
                {   
                    String tem = result.getString("name");
                    listCS.add(tem);                                    

                }//end of while
            }//end of if

    }//end of try
    catch(java.sql.SQLException e)
    {
        e.printStackTrace();
    }
    return listCS;
}

最佳答案

第一个函数的错误表明您正在使用 JNDI 命名资源来获取连接,而不是来自 DriverManager 的资源。

您确定设备可以看到该主机和端口吗?设备是否有连接权限? MySQL数据库中的GRANT是什么样的?

您从类别表中获取名称的方法也不好。您不会在finally block 中关闭任何资源。你应该。

那些 //end of while//end of if 注释是令人发指的。它们不会添加信息,只会造成困惑。从您的代码中删除这些内容。

关于java - sql 连接错误 - 阻止我从 mysql 获取 'SELECT' 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744054/

相关文章:

java - VisualForce 编程不会产生输出

java - Spring Framework 中的@Inject 和@Autowired 有什么区别?在什么条件下使用哪一种?

java - 使用 Android 播放来自 jar 资源的音频文件

Android与数据可视化: How to present data in a better way?

java - java中如何将多个字符串解析为int

android - 带有 Android 模拟器的 TouchUtils

java - 图像查看器中每个图像的不同文本

mysql - 远程连接到 Google Cloud SQL

php - 警告 : Invalid argument supplied for foreach() in

php - 从mysql中的另一个表获取数据