win10 下Android 连接电脑本地MySQL数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/github_38482082/article/details/80801956

1.确认MySQL服务器防火墙关闭,若关闭,则下一步,若未关闭,进行设置

具体方法为

控制面板->系统和安全>Windows Defender 防火墙->高级设置->入站规则

选择端口进入下一步,选择TCP,特定本地端口80,3306下一步,全选,确定,此时防火墙打开





2. MySQL除了root用户还应新建一个其他用户,root用户只能用于本地本机连接数据库,如有其他用户,此步忽略

    具体方法为

    1)打开dos窗口,进入到MySQL的bin目录,再用root账户登陆

    mysql -h localhost -u root -p密码

    mysql命令模式

    2)use mysql;

    3)create user 'userName'@'host' identified by 'password';

说明:

  • userName:创建数据库新用户的用户名
  • host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost;如果想让该用户可以从任意远程主机登陆,可以使用通配符%
  • password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

3. 授权用户数据库操作权限

    grant privileges on databaseName.tableName to 'userName'@'host';

说明:

  • privileges:用户的操作权限,如SELECTINSERTUPDATE等,如果要授予所的权限则使用all privileges 
  • databaseName:数据库名
  • userName:指定需要赋予权限的用户
  • tableName:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*
  • host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost;如果想让该用户可以从任意远程主机登陆,可以使用通配符%

最后,代码参照Java连接MySQL

注意:Android项目要导入MySQL的jar包

另外连接数据是耗时操作,Android4.0后必须使用子线程进行处理

展开阅读全文

android模拟器访问本地电脑Mysql数据库出错

09-02

我的application程序如下rnpublic class MainActivity extends Activity rn Button btn;rn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.activity_main);rn btn = (Button) findViewById(R.id.button1);rn btn.setOnClickListener(new OnClickListener() rn @Overridern public void onClick(View v) rn try rn String sql="select * from test"; rn Statement statement = (Statement) getConnection().createStatement();rn getConnection().close();rn PreparedStatement psmt=(PreparedStatement) getConnection().prepareStatement(sql);rn ResultSet rs=psmt.executeQuery();rn while(rs.next())rn System.out.println(rs.getString(1));rn rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn ); rn rn public static Connection getConnection() throws Exception rn String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动rn String dbURL = "jdbc:mysql://10.0.0.2:3306/localhost_3306?auotReconnect=true&useUnicode=true&characterEncoding=utf8"; // 连接服务器和数据loginrn // System.out.print(dbURL);rn String userName = "root"; // 用户名rn String userPwd = "kaka3511"; // 密码rn Class.forName(driverName);rn return (Connection) DriverManager.getConnection(dbURL, userName, userPwd);rn rnrnMysql数据库内容如下rn[img=https://img-bbs.csdn.net/upload/201609/02/1472806790_90869.jpg][/img]rn运行之后,当点击button之后,Logcat出现如下信息rn09-02 08:57:24.484: I/Choreographer(1224): Skipped 39 frames! The application may be doing too much work on its main thread.rn09-02 08:57:24.744: W/System.err(1224): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failurern09-02 08:57:24.774: W/System.err(1224): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.rn09-02 08:57:24.824: W/System.err(1224): at java.lang.reflect.Constructor.constructNative(Native Method)rn09-02 08:57:24.834: W/System.err(1224): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)rn09-02 08:57:24.844: W/System.err(1224): at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)rn09-02 08:57:24.844: W/System.err(1224): at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)rn09-02 08:57:24.854: W/System.err(1224): at com.mysql.jdbc.MysqlIO.(MysqlIO.java:357)rn09-02 08:57:24.854: W/System.err(1224): at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)rn09-02 08:57:24.864: W/System.err(1224): at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)rn09-02 08:57:24.864: W/System.err(1224): at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)rn09-02 08:57:24.864: W/System.err(1224): at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:834)rn09-02 08:57:24.874: W/System.err(1224): at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)rn09-02 08:57:24.874: W/System.err(1224): at java.lang.reflect.Constructor.constructNative(Native Method)rn09-02 08:57:24.884: W/System.err(1224): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)rn09-02 08:57:24.884: W/System.err(1224): at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)rn09-02 08:57:24.904: W/System.err(1224): at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)rn09-02 08:57:24.904: W/System.err(1224): at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)rn09-02 08:57:24.915: W/System.err(1224): at java.sql.DriverManager.getConnection(DriverManager.java:175)rn09-02 08:57:24.915: W/System.err(1224): at java.sql.DriverManager.getConnection(DriverManager.java:209)rn09-02 08:57:24.924: W/System.err(1224): at com.example.testmysql.MainActivity.getConnection(MainActivity.java:53)rn09-02 08:57:24.934: W/System.err(1224): at com.example.testmysql.MainActivity$1.onClick(MainActivity.java:30)rn09-02 08:57:24.964: W/System.err(1224): at android.view.View.performClick(View.java:4084)rn09-02 08:57:25.004: W/System.err(1224): at android.view.View$PerformClick.run(View.java:16966)rn09-02 08:57:25.004: W/System.err(1224): at android.os.Handler.handleCallback(Handler.java:615)rn09-02 08:57:25.004: W/System.err(1224): at android.os.Handler.dispatchMessage(Handler.java:92)rn09-02 08:57:25.014: W/System.err(1224): at android.os.Looper.loop(Looper.java:137)rn09-02 08:57:25.014: W/System.err(1224): at android.app.ActivityThread.main(ActivityThread.java:4745)rn09-02 08:57:25.024: W/System.err(1224): at java.lang.reflect.Method.invokeNative(Native Method)rn09-02 08:57:25.044: W/System.err(1224): at java.lang.reflect.Method.invoke(Method.java:511)rn09-02 08:57:25.044: W/System.err(1224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)rn09-02 08:57:25.104: W/System.err(1224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)rn09-02 08:57:25.104: W/System.err(1224): at dalvik.system.NativeStart.main(Native Method)rn09-02 08:57:25.104: W/System.err(1224): Caused by: java.net.SocketException: android.os.NetworkOnMainThreadExceptionrn09-02 08:57:25.154: W/System.err(1224): at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:410)rn09-02 08:57:25.154: W/System.err(1224): at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:271)rn09-02 08:57:25.174: W/System.err(1224): at com.mysql.jdbc.MysqlIO.(MysqlIO.java:307)rn09-02 08:57:25.174: W/System.err(1224): ... 25 morern求大神指教为啥查询不了rn 论坛

没有更多推荐了,返回首页