cry8801 2018-03-03 19:10 采纳率: 100%
浏览 1911
已采纳

Android 通过jdbc 直连MySql 连不上

本人新手,找了好多资源,测试直连的问题,这些代码也是本网站找的,自己稍微改了一下,为何死活连不上去,conn返回都是空的,大神帮我看看。
public class MainActivity extends Activity {

private static final String URL = "jdbc:mysql://www.luhef.cn:3306/xh";
private static final String USER = "*******";
private static final String PASSWORD = "*****";

private Connection conn;
private Button onConn;
private Button onInsert;
private Button onDelete;
private Button onUpdate;
private Button onQuery;
private Handler handler = new Handler() {

    public void handleMessage(Message msg) {
        String str = (String) msg.obj;
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
    };
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onConn = (Button) findViewById(R.id.onConn);
    onInsert = (Button) findViewById(R.id.onInsert);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            conn = null;
        } finally {
            conn = null;
        }
    }
}

public void onConn(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            conn = Util.openConnection(URL, USER, PASSWORD);
            Message msg = new Message();
            msg.obj = conn;
            handler.sendMessage(msg);
        }
    }).start();
}

public void onInsert(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            String sql = "insert into users values(15, 'xiaoming')";
            Util.execSQL(conn, sql);
            Log.i("onInsert", "onInsert");
        }
    }).start();
}

}

package com.lyz.learning.mysqlandandroid;

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

public class Util {
final static String DRIVER_NAME = "com.mysql.jdbc.Driver";

public static Connection openConnection(String url, String user,
                                        String password) {
    Connection conn = null;
    try {
        Class.forName(DRIVER_NAME);
        conn = DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
        conn = null;
    } catch (SQLException e) {
        conn = null;
    }
    System.out.println(conn);

    return conn;
}

public static void query(Connection conn, String sql) {
    if (conn == null) {
        return;
    }
    Statement statement = null;
    ResultSet result = null;
    try {
        statement = conn.createStatement();
        result = statement.executeQuery(sql);
        System.out.println(result);
        if (result != null && result.first()) {
            int idColumnIndex = result.findColumn("id");
            int nameColumnIndex = result.findColumn("name");
            System.out.println("id\t\t" + "name");
            while (!result.isAfterLast()) {
                System.out.print(result.getString(idColumnIndex) + "\t\t");
                System.out.println(result.getString(nameColumnIndex));
                result.next();
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (result != null) {
                result.close();
                result = null;
            }
            if (statement != null) {
                statement.close();
                statement = null;
            }} catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
}

public static boolean execSQL(Connection conn, String sql) {
    boolean execResult = false;
    if (conn == null) {
        return execResult;
    }
    Statement statement = null;
    try {
        statement = conn.createStatement();
        if (statement != null) {
            execResult = statement.execute(sql);
        }
    } catch (SQLException e) {
        execResult = false;
    }
    return execResult;
}

}

展开全部

  • 写回答

10条回答 默认 最新

  • davi_qiang 2018-03-04 00:13
    关注

    是否在Manifest中配置了相关的网络权限和读写权限?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部