您好,欢迎访问一九零五行业门户网

jdbc操作数据库语句

非常有用的jdbc操作数据库语句,记录下来,以方便以后的查询。 public class persondao { // 增加操作 public void insert(person person) throws exception; // 修改操作 public void update(person person) throws exception; // 删除操作 public void del
非常有用的jdbc操作数据库语句,记录下来,以方便以后的查询。
public class persondao { // 增加操作 public void insert(person person) throws exception; // 修改操作 public void update(person person) throws exception; // 删除操作 public void delete(string id) throws exception ; // 按id查询操作 public person querybyid(string id) throws exception; // 查询全部 public list queryall() throws exception; // 模糊查询 public list querybylike(string cond) throws exception;}
// 此类需要完成具体的数据库操作,需要jdb代码 public class persondaoimpl implements persondao { // 增加操作 public void insert(person person) throws exception { string sql = insert into person (id,name,password,age,email) values (?,?,?,?,?) ; preparedstatement pstmt = null ; databaseconnection dbc = null ; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new databaseconnection() ; pstmt = dbc.getconnection().preparestatement(sql) ; pstmt.setstring(1,person.getid()) ; pstmt.setstring(2,person.getname()) ; pstmt.setstring(3,person.getpassword()) ; pstmt.setint(4,person.getage()) ; pstmt.setstring(5,person.getemail()) ; // 进行数据库更新操作 pstmt.executeupdate() ; pstmt.close() ; } catch (exception e) { throw new exception(操作出现异常) ; } finally { // 关闭数据库连接 dbc.close() ; } } // 修改操作 public void update(person person) throws exception { string sql = update person set name=?,password=?,age=?,email=? where id=? ; preparedstatement pstmt = null ; databaseconnection dbc = null ; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new databaseconnection() ; pstmt = dbc.getconnection().preparestatement(sql) ; pstmt.setstring(1,person.getname()) ; pstmt.setstring(2,person.getpassword()) ; pstmt.setint(3,person.getage()) ; pstmt.setstring(4,person.getemail()) ; pstmt.setstring(5,person.getid()) ; // 进行数据库更新操作 pstmt.executeupdate() ; pstmt.close() ; } catch (exception e) { throw new exception(操作出现异常) ; } finally { // 关闭数据库连接 dbc.close() ; } } // 删除操作 public void delete(string id) throws exception { string sql = delete from person where id=? ; preparedstatement pstmt = null ; databaseconnection dbc = null ; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new databaseconnection() ; pstmt = dbc.getconnection().preparestatement(sql) ; pstmt.setstring(1,id) ; // 进行数据库更新操作 pstmt.executeupdate() ; pstmt.close() ; } catch (exception e) { throw new exception(操作出现异常) ; } finally { // 关闭数据库连接 dbc.close() ; } } // 按id查询操作 public person querybyid(string id) throws exception { person person = null ; string sql = select id,name,password,age,email from person where id=? ; preparedstatement pstmt = null ; databaseconnection dbc = null ; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new databaseconnection() ; pstmt = dbc.getconnection().preparestatement(sql) ; pstmt.setstring(1,id) ; // 进行数据库查询操作 resultset rs = pstmt.executequery() ; if(rs.next()) { // 查询出内容,之后将查询出的内容赋值给person对象 person = new person() ; person.setid(rs.getstring(1)) ; person.setname(rs.getstring(2)) ; person.setpassword(rs.getstring(3)) ; person.setage(rs.getint(4)) ; person.setemail(rs.getstring(5)) ; } rs.close() ; pstmt.close() ; } catch (exception e) { throw new exception(操作出现异常) ; } finally { // 关闭数据库连接 dbc.close() ; } return person ; } // 查询全部 public list queryall() throws exception { list all = new arraylist() ; string sql = select id,name,password,age,email from person ; preparedstatement pstmt = null ; databaseconnection dbc = null ; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new databaseconnection() ; pstmt = dbc.getconnection().preparestatement(sql) ; // 进行数据库查询操作 resultset rs = pstmt.executequery() ; while(rs.next()) { // 查询出内容,之后将查询出的内容赋值给person对象 person person = new person() ; person.setid(rs.getstring(1)) ; person.setname(rs.getstring(2)) ; person.setpassword(rs.getstring(3)) ; person.setage(rs.getint(4)) ; person.setemail(rs.getstring(5)) ; // 将查询出来的数据加入到list对象之中 all.add(person) ; } rs.close() ; pstmt.close() ; } catch (exception e) { throw new exception(操作出现异常) ; } finally { // 关闭数据库连接 dbc.close() ; } return all ; } // 模糊查询 public list querybylike(string cond) throws exception { list all = new arraylist() ; string sql = select id,name,password,age,email from person where name like ? or email like ? ; preparedstatement pstmt = null ; databaseconnection dbc = null ; // 下面是针对数据库的具体操作 try { // 连接数据库 dbc = new databaseconnection() ; pstmt = dbc.getconnection().preparestatement(sql) ; // 设置模糊查询条件 pstmt.setstring(1,%+cond+%) ; pstmt.setstring(2,%+cond+%) ; // 进行数据库查询操作 resultset rs = pstmt.executequery() ; while(rs.next()) { // 查询出内容,之后将查询出的内容赋值给person对象 person person = new person() ; person.setid(rs.getstring(1)) ; person.setname(rs.getstring(2)) ; person.setpassword(rs.getstring(3)) ; person.setage(rs.getint(4)) ; person.setemail(rs.getstring(5)) ; // 将查询出来的数据加入到list对象之中 all.add(person) ; } rs.close() ; pstmt.close() ; } catch (exception e) { throw new exception(操作出现异常) ; } finally { // 关闭数据库连接 dbc.close() ; } return all ; } };
ps:1.pstmt操作时,第一个空格处,应该填写1,而不是0,否则会报错误的列索引;
2.查询完resultset以后,先要对其进行非空判断,如果存在,再进行下一步的赋值;
3.数据库每次连接时,都要记住关闭,当然完全可以用数据库连接池来取代它,后面会对此做改进。
其它类似信息

推荐信息