publicclassSqlHelper{privateConnectioncon=null;//连接privateResultSetrs=null;//结果集privatePreparedStatementpsm=null;privatestaticStringuserName="";privatestaticStringpa...
public class SqlHelper {
private Connection con = null;// 连接
private ResultSet rs = null;// 结果集
private PreparedStatement psm = null;
private static String userName = "";
private static String passWord = "";
private static String driver = "";
private static String url = "";
private static InputStream fis = null;
static {
Properties pp = new Properties();
try {
fis = SqlHelper.class.getClassLoader().getResourceAsStream(
"/mysql.properties");
pp.load(fis);
userName = pp.getProperty("userName");
passWord = pp.getProperty("passWord");
driver = pp.getProperty("driver");
url = pp.getProperty("url");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Connection getConnection() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, userName, passWord);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
mysql.properties文件放在同一目录
展开