|
package com.fgkj.db;
|
|
import java.sql.Connection;
|
import java.sql.DriverManager;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.sql.Statement;
|
|
import com.fgkj.services.ServiceModel;
|
|
|
public class DBUtil1 {
|
|
private static String classDriver="com.mysql.jdbc.Driver";
|
// private static String url="jdbc:mysql://192.168.0.34:5306/";
|
// private static String ip="192.168.0.34";
|
private static String ip="211.149.178.177";
|
// private static String ip = "127.0.0.1";
|
private static String username = "root";
|
private static String password = "lmx8688139";
|
|
// private static String url="jdbc:mysql://192.168.0.34:5306/";
|
// private static String username="root";
|
// private static String password="lmx8688139";
|
|
private static String port = ":5307/";
|
private static String url = "jdbc:mysql://";
|
private static String reConnect="?autoReconnect=true";
|
// private static String username="root";
|
// private static String password="lmx8688139";
|
|
public static Connection getConn(String databaseName){
|
Connection conn=null;
|
try {
|
Class.forName(classDriver);
|
DriverManager.setLoginTimeout(10);
|
conn = DriverManager.getConnection(url+ip+port+databaseName+reConnect, username, password);
|
} catch (ClassNotFoundException e) {
|
e.printStackTrace();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
return conn;
|
}
|
|
public static Connection getConn(){
|
Connection conn=null;
|
try {
|
Class.forName(classDriver);
|
DriverManager.setLoginTimeout(10);
|
conn = DriverManager.getConnection(url+ip+port+reConnect, username, password);
|
} catch (ClassNotFoundException e) {
|
e.printStackTrace();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
return conn;
|
}
|
|
//关闭资源
|
public static void close(ResultSet rs,Statement stat,Connection conn){
|
try {
|
if(rs != null)rs.close();
|
if(stat != null)stat.close();
|
if(conn != null)conn.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
|
public static String getIp() {
|
return ip;
|
}
|
|
public static ServiceModel setIp(String ip) {
|
ServiceModel model = new ServiceModel();
|
Connection conn = null;
|
try {
|
conn = DriverManager.getConnection(url + ip + port,username, password);
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
if(conn != null){
|
model.setCode(1);
|
model.setMsg("修改成功!");
|
DBUtil1.ip=ip;
|
}else{
|
model.setCode(0);
|
model.setMsg("修改失败请检查该ip服务器上是否配置了数据库!");
|
}
|
System.out.println(model);
|
return model;
|
}
|
|
public static void main(String[] args){
|
System.out.println(getConn(IDatabaseName.DB_BATT_TESTDATA));
|
System.out.println(getConn(IDatabaseName.DB_BATT_TESTDATA));
|
|
// Connection conn=getConn(IDatabaseName.DB_BATTINF);
|
// try {
|
// PreparedStatement ps=conn.prepareStatement("select count(*) from tb_battinf");
|
// ResultSet rs=ps.executeQuery();
|
// while(rs.next()){
|
// int count=rs.getInt(1);
|
// System.out.println("查询成功"+count);
|
// }
|
// } catch (SQLException e) {
|
// e.printStackTrace();
|
// }
|
|
}
|
}
|