使用jdbc,代码如下: package com.example.Mysql; import java.sql.Connection; import
使用jdbc,代码如下: package com.example.Mysql; 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 " + "name"); while (!result.isAfterLast()) { System.out.print(result.getString(idColumnIndex) + " "); 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; } }
--结束END--
本文标题: Android连接MySQL方法,测试成功
本文链接: https://lsjlt.com/news/28676.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0