chn2212205 2016-01-16 06:03 采纳率: 0%
浏览 1964

安卓新手用JDBC连接mysql时一直报错

抛出异常在logcat提示:驱动连接成功,数据库连接失败Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo。求大神帮忙,错误围绕好长时间了

连接代码

 //连接数据库
 public static Connection getCon()
 {
  Connection conn = null;
  try {
   Class.forName("org.gjt.mm.mysql.Driver");
   System.out.println("驱动连接成功");
  } catch (ClassNotFoundException e1) {
   // TODO Auto-generated catch block
   System.out.println("驱动连接失败");
  }
     String dbUrl = "jdbc:mysql://localhost:3306/student";
     String username = "root";
     String psw = "1234";
  try {
   conn = DriverManager.getConnection(dbUrl,username,psw);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   System.out.println("数据库连接失败"+e);
  }
  return conn;
 }
  • 写回答

1条回答 默认 最新

  • save4me 2016-01-16 15:09
    关注

    参考Connecting to MySQL from Android with JDBC
    把数据库的部分放到AsyncTask或者Thread里面

    public void mysql() {
    
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    
        thrd1 = new Thread(new Runnable() {
            public void run() {
                while (!Thread.interrupted()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e1) {
    
                    }
                    if (con == null) {
                        try {
                            con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345");
                        } catch (SQLException e) {
                            e.printStackTrace();
                            con = null;
                        }
    
                        if ((thrd2 != null) && (!thrd2.isAlive()))
                            thrd2.start();
                    }
                }
            }
        });
        if ((thrd1 != null) && (!thrd1.isAlive())) thrd1.start();
    
        thrd2 = new Thread(new Runnable() {
            public void run() {
                while (!Thread.interrupted()) {
    
                    if (con != null) {
                        try {
                         //   con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345");
                            Statement st = con.createStatement();
                            String ali = "'fff'";
                            st.execute("INSERT INTO deneme (name) VALUES(" + ali + ")");
                          //  ResultSet rs = st.executeQuery("select * from deneme");
                          //  ResultSetMetaData rsmd = rs.getMetaData();
                          //  String result = new String();
    
                          //  while (rs.next()) {
                          //      result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
                         //       result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
                         //   }
    
                        } catch (SQLException e) {
                            e.printStackTrace();
                            con = null;
                        }
    
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败