public class MySQL_DBUtils {
private static String driver = “com.mysql.jdbc.Driver”; // MySql驱动
private static String ip = “172.16.xxx.xxx”; // 写成本机地址,不能写成localhost,同时手机和电脑连接的网络必须是同一个
private static String port = “3306”;
private static String user = “123”; // 用户名
private static String password = “123”; // 密码
private static String dbName = “test”;//数据库名称
private static String tag = “MySQL_DBUtils”;
// 获取数据库的连接
private static Connection getConn(){
Connection connection = null;
try{
Class.forName(driver);// 动态加载类
// 尝试建立到给定数据库URL的连接 +“?zeroDateTimeBehavior=convertToNull”
connection = DriverManager.getConnection(“jdbc:mysql://” + ip + “:”+ port +“/”+dbName+“?zeroDateTimeBehavior=convertToNull”,user, password);
Log.i(tag, “database " + ip + " " + dbName + " connect success…”);
}catch (Exception e){
Log.i(tag, “database " + ip + " " + dbName + " connect fail…”);
e.printStackTrace();
}
return connection;
}
}
//调用
Connection connection = MySQL_DBUtils.getConn();
java连接MySQL
最新推荐文章于 2024-05-05 06:54:30 发布