首先你得确定你的数据库连接是通过什么形式连接的,hibernate还是原生态的jdbc 还是spring;
如果是只有hibernate,那么你得通过加载配置文件得到sessionFactory,然后得到session
如果spring,那么同样也需要注入sessionfactory到你的dao
如果是jdbc方式,那么你就按照原生态jdbc写法
总之,在你构造DAO时,得有数据源。这样才能 *** 纵你的数据库
如果搞懂了这些问题,那么你的第一个,第三个问题就迎刃而解了。至于第二问题,我没明白你什么意思!
1.增加\x0d\x0aString s1="insert into tableNames (id,name,password) values(myseq.nextval,?,?)"\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s1)\x0d\x0aprepStmt.setString(1,name)\x0d\x0aprepStmt.setString(2,password)\x0d\x0aResultSet rs=stmt.executeUpdate()\x0d\x0a2、删除\x0d\x0aString s2="delete from tbNames where name=?"\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s2)\x0d\x0aprepStmt.setString(1,name)\x0d\x0aResultSet rs=stmt.executeUpdate()\x0d\x0a3、修改\x0d\x0aString s3=“update tbNames set name=? where id=?”\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s3)\x0d\x0aprepStmt.setString(1,name)\x0d\x0aprepStmt.setString(2,id)\x0d\x0aResultSet rs=stmt.executeUpdate()\x0d\x0a4、查询\x0d\x0aString s4="select id,name,password from tbNames"\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aStatement stmt=conn.createStatement()\x0d\x0aResultSet rs = stmt.executeQuery(s4)\x0d\x0awhile(rs.next){\x0d\x0aint id=rs.getInt(1)\x0d\x0aString name = rs.getString(2)\x0d\x0aString pwd=rs.getString(3)\x0d\x0aSystem.out.println(id+name+pwd)} \x0d\x0a\x0d\x0a以上四步必须都得关闭连接;!!!\x0d\x0ars.close()\x0d\x0astmt.close()\x0d\x0aconn.close()欢迎分享,转载请注明来源:内存溢出
评论列表(0条)