久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

servlet中MySQL數(shù)據(jù)庫(kù)的增,、刪,、查,、改

 崋果山 2017-09-29
//添加user
public void addUser(User user) throws Exception{
//通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接
Connection conn = null;
DataSource ds = null;
Context initCtx;
try {
initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test");
if(ds!=null){
conn = ds.getConnection();
}
} catch (NamingException e) {
e.printStackTrace();
}
String sql=""+
"insert into account"+
"(name,password)"+
"values("+
"?,?)";
//2.?通過(guò)數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù)
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1,user.getName());
ptmt.setString(2,user.getPassword());
ptmt.execute();
//3.添加語(yǔ)句
//ResultSet rs=stmt.executeQuery("insert into account('name','password')value('wocao','12345')");
}
//更改user
public void updateUser(User user) throws SQLException{
//通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接
Connection conn = null;
DataSource ds = null;
Context initCtx;
try {
initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test");
if(ds!=null){
conn = ds.getConnection();
}
} catch (NamingException e) {
e.printStackTrace();
}
String sql=""+
" update account"+
" set name=?,password=?  "+
" where id=? ";
//2.?通過(guò)數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù)
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1,user.getName());
ptmt.setString(2, user.getPassword());
ptmt.setInt(3,user.getId());
ptmt.execute();
}
//刪除user
public void delUser(Integer id) throws SQLException{
//通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接
Connection conn = null;
DataSource ds = null;
Context initCtx;
try {
initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test");
if(ds!=null){
conn = ds.getConnection();
}
} catch (NamingException e) {
e.printStackTrace();
}
String sql=""+
" delete from account "+
" where id=? ";
//2.通過(guò)數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù)
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setInt(1, id);
ptmt.execute();
}
//查詢user
public List<User> query() throws Exception{
//通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接
Connection conn = null;
DataSource ds = null;
Context initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test");
if(ds!=null){
conn = ds.getConnection();
}
//2.通過(guò)連接池操作數(shù)據(jù)庫(kù)
Statement stmt= conn.createStatement();
//3.查詢語(yǔ)句
ResultSet rs=stmt.executeQuery("select * from account");
//4..創(chuàng)建一個(gè)將要返回的List集合對(duì)象list,,一個(gè)User的對(duì)象u
List<User> list=new ArrayList<User>();
User u = null;
//5.重設(shè)用戶User對(duì)象u中的屬性,,然后將重設(shè)后的對(duì)象u添加到集合對(duì)象list中
while(rs.next()){
u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
list.add(u);
}
return list;
}
public User get(Integer id) throws SQLException{
//通過(guò)tomcat連接池獲得數(shù)據(jù)庫(kù)的連接
User user=null;
Connection conn = null;
DataSource ds = null;
PreparedStatement ptmt=null;
ResultSet rs=null;
Context initCtx;
try {
initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/test");
if(ds!=null){
conn = ds.getConnection();
}
String sql=""+
" select * from account"+
" where id=? ";
//2.通過(guò)連接池的連接操作數(shù)據(jù)庫(kù)
ptmt=conn.prepareStatement(sql);
ptmt.setInt(1,id);
rs=ptmt.executeQuery();
//conn.close();
hile(rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
}
} catch (NamingException e) {
e.printStackTrace();
}finally{
rs.close();
ptmt.close();
conn.close();
}
return user;
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多