首页 > 数据库 > 其他综合 >

利用web.xml配置数据库

2017-04-24

利用web xml配置数据库。

1.web.xml 配置

dbUrl

jdbc:mysql://localhost:8080/auth

driverName

com.jdbc.mysql.Driver

userName

root

passWord

123456

2.初始化

private String dbUrl;

private String driverName;

private String userName;

private String passWord;

private Connection conn=null;

private PreparedStatement ps=null;

private ResultSet rs=null;

@Override

//初始化

public void init(ServletConfig config) throws ServletException {

//读取配置文件里面的信息

this.dbUrl=config.getInitParameter("dbUrl");

this.driverName=config.getInitParameter("driverName");

this.userName=config.getInitParameter("userName");

this.passWord=config.getInitParameter("passWord");

}

3.操作

3.1.查询

public String getNickName(String openid) throws SQLException{

String nickName="";

conn = DriverManager.getConnection(dbUrl,userName,passWord);

String sql="select * from user where OPENID=?";

ps = conn.prepareStatement(sql);

ps.setString(1, openid);

rs = ps.executeQuery();

while(rs.next()){

nickName=rs.getString("NICKNAME");

}

rs.close();

ps.close();

conn.close();

return nickName;

}

3.2 更新

public int updUser(String openid,String userName,String passWord) throws SQLException{

String nickName="";

conn = DriverManager.getConnection(dbUrl,userName,passWord);

String sql="update user set OPENID=? where USERNAME=? and PASSWORD=?";

ps = conn.prepareStatement(sql);

ps.setString(1, openid);

ps.setString(2, userName);

ps.setString(3, passWord);

int temp=ps.executeUpdate();

rs.close();

ps.close();

conn.close();

return temp;

}

相关文章
最新文章
热点推荐