Android连接MySQL方法,测试成功
作者:网络转载 发布时间:[ 2017/3/16 10:37:33 ] 推荐标签:数据库 MySQL Android
使用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;
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11