这篇文章记录Android直连mysql数据库,直连SqlServer数据库
直连mysql数据库
采用JDBC的方式
第一步:导入mysql架包,下载地址: mysql架包 提取码: iybw
第二步:配置信息
public class Constant {
/**
* 联网数据库驱动
*/
public static final String SQL_DEVICES = "com.mysql.jdbc.Driver";
/**
* 连接数据库条件
*/
public static final String SQL_CONNECTION = "jdbc:mysql://服务器地址:端口号/数据库名称";
/**
* 数据库用户名
*/
public static final String SQL_USERNAME = "用户名";
/**
* 数据库密码
*/
public static final String SQL_PASSWORD = "密码";
}
第三步:可新建类用于增删改查操作:
//查询操作
public static ResultSet result(String sql) throws Exception {
Class.forName(SQL_DEVICES).newInstance(); //加载MYSQL JDBC驱动程序
Connection connection = (Connection) DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME,SQL_PASSWORD);
Statement statement = (Statement) connection.createStatement();
return statement.executeQuery(sql);
}
//插入操作
public static boolean executeInsert(final String insertSql) {
try {
Class.forName(SQL_DEVICES).newInstance();
Connection connection = DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME, SQL_PASSWORD);
Statement statement = connection.createStatement();
return statement.execute(insertSql);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
//值为1成功 更新操作
public static int executeUpdate(final String updateSql) {
try {
Class.forName(SQL_DEVICES).newInstance();
Connection connection = DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME, SQL_PASSWORD);
Statement statement = connection.createStatement();
return statement.executeUpdate(updateSql);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
至此,mysql的配置以及操作就完成了.
直连SqlServer数据库
采用jtds的方式
导入架包至libs文件夹,下载地址:jtds架包下载 提取码: gbvm
配置信息
public class Constant {
/**
* 联网数据库驱动
*/
public static final String SQL_DEVICES = "net.sourceforge.jtds.jdbc.Driver";
/**
* 连接数据库条件
*/
public static final String SQL_CONNECTION = "jdbc:jtds:sqlserver://服务器地址:端口/数据库名称;charset=utf8";
/**
* 数据库用户名
*/
public static final String SQL_USERNAME = "用户名";
/**
* 数据库密码
*/
public static final String SQL_PASSWORD = "密码";
}
具体的数据操作
public class JtdsHttp {
private static JtdsHttp instance = new JtdsHttp();
public static JtdsHttp getInstance() {
return instance;
}
//查询
public ResultSet executeQuery(final String querySql) {
try {
Class.forName(SQL_DEVICES).newInstance();
Connection connection = DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME, SQL_PASSWORD);
Statement statement = connection.createStatement();
return statement.executeQuery(querySql);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//插入
public boolean executeInsert(final String insertSql) {
try {
Class.forName(SQL_DEVICES).newInstance();
Connection connection = DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME, SQL_PASSWORD);
Statement statement = connection.createStatement();
return statement.execute(insertSql);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
//值为1成功 更新
public int executeUpdate(final String updateSql) {
try {
Class.forName(SQL_DEVICES).newInstance();
Connection connection = DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME, SQL_PASSWORD);
Statement statement = connection.createStatement();
return statement.executeUpdate(updateSql);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
//删除
public boolean executeDelete(final String deleteSql) {
try {
Class.forName(SQL_DEVICES).newInstance();
Connection connection = DriverManager.getConnection(
SQL_CONNECTION, SQL_USERNAME, SQL_PASSWORD);
Statement statement = connection.createStatement();
return statement.execute(deleteSql);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
直连SqlServer的方式就这些了,跟mysql的差不多
网友评论