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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。