public class JdbcUtil {
private static JdbcUtil jdbcUtil;
private Connection con = null;
private String url = "jdbc:mysql://127.0.0.1:3306/quotationmanage?characterEncoding=utf8&useSSL=false";
private String user = "root";
private String pass = "root";
private JdbcUtil() {
super();
// try {
// File file = new File("jdbcInfo.properties");
// InputStream is = new FileInputStream(file);
// Properties pp = new Properties();
// pp.load(is);
// url = pp.getProperty("url");
// user = pp.getProperty("user");
// pass = pp.getProperty("pass");
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public Connection getConnection() {
try {
con = DriverManager.getConnection(url, user, pass);
con.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static JdbcUtil getInstance() {
if (jdbcUtil == null) {
jdbcUtil = new JdbcUtil();
}
return jdbcUtil;
}
//关闭资源
public void close(ResultSet rs, Statement st, Connection con) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}