数据库连接怎样怎样在配置文件中配置? 写了个小网站,每次连接数据库都得改端口号,在代码中改有点麻烦,请问怎样将连接数据库的类在配置文件中配置好??最好有一个典型的列子,谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就是写个配置文件,然后用java来读取配置文件,这样代码网上很多,搜一下 可以写个工具类public class DBConnection { public static Connection getConnection(){ String url="jdbc:mysql://localhost:3306/gwap"; String DriverName="com.mysql.jdbc.Driver"; String username="root"; String password="mysql"; Connection conn=null; try{ Class.forName(DriverName); System.out.println("********开始进行连接********"); conn=DriverManager.getConnection(url, username, password); System.out.println("********数据库连接成功********"); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(Exception e1){ System.out.println("********数据库连接失败********"); e1.printStackTrace(); } return conn; } public static void closeConnection(Connection conn){ if(conn!=null){ try { conn.close(); } catch (SQLException e) { throw new RuntimeException("ERROR.UNEXPECTED"); } } } public static void closeStatement(Statement stmt){ if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { throw new RuntimeException("ERROR.UNEXPECTED"); } } } public static void closePreparedStatement(PreparedStatement pstmt){ if(pstmt!=null){ try { pstmt.close(); } catch (SQLException e) { throw new RuntimeException("ERROR.UNEXPECTED"); } } } public static void closeResultSet(ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { throw new RuntimeException("ERROR.UNEXPECTED"); } } } public static void main(String args[]){ DBConnection.getConnection(); }} 用hibernate 可以些配置文件 脱离了java web的开发终于做出了决定,再次散分! 如何在一个js文件中引入另一个js? 急急急,JSP上传超大文件的问题??? jsp取值的一个简单问题 急救!在线等,谢谢各位大神 tomcat7启动慢 报错不影响运行 想解决报错 求指导 如何防止服务器被恶意攻击? JSP中变量类型转换问题 跨平台问题:怎样用程序得到JSP服务器的默认编码? tomcat7 与redis整合 session问题 想问一下jsp中request的问题 中文乱码问题
public class DBConnection {
public static Connection getConnection(){
String url="jdbc:mysql://localhost:3306/gwap";
String DriverName="com.mysql.jdbc.Driver";
String username="root";
String password="mysql";
Connection conn=null;
try{
Class.forName(DriverName);
System.out.println("********开始进行连接********");
conn=DriverManager.getConnection(url, username, password);
System.out.println("********数据库连接成功********");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(Exception e1){
System.out.println("********数据库连接失败********");
e1.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
}
public static void closeStatement(Statement stmt){
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
}
public static void closePreparedStatement(PreparedStatement pstmt){
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
}
public static void closeResultSet(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
}
public static void main(String args[]){
DBConnection.getConnection();
}
}
用hibernate 可以些配置文件