可以通过reflect反射去做,具体可以参见sqlmapper
非常简单,只有一个文件就搞定了
基础的增删改查都包含了,连硬编码的sql string都不用写,非常方便
如何从mysql数据库生成类实体myeclipse从数据库反射生成实体类和映射文件: 创建数据库,创建相应的表 点击图标,选择MyEclipse Datebase Explorer 右击空白区域,选择new菜单,根据提示创建数据库连接,创建好后会显示你所创建的连接名,如图mysqldb是我创建的数据库连接
//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释\x0d\x0aString sql = "select username,password from account"\x0d\x0aString user = request.getParameter("user")\x0d\x0aString pass = request.getParameter("password")\x0d\x0aint j = 0\x0d\x0aConnection conn = null\x0d\x0aPreparedStatement ps = null\x0d\x0aResultSet rs = null\x0d\x0atry {\x0d\x0aconn = JDBCTools1.getConnection()\x0d\x0aps = conn.prepareStatement(sql)\x0d\x0ars = ps.executeQuery()\x0d\x0a//从表中查询获取所有账户的用户名&密码的ResultSet 对象\x0d\x0awhile(rs.next()){\x0d\x0aint i = 0\x0d\x0a\x0d\x0aString username[] = new String[10]//用户名数组\x0d\x0aString password[] = new String[10]//密码数组\x0d\x0ausername[i] = rs.getString(1)\x0d\x0apassword[i] = rs.getString(2)\x0d\x0aif(user.equals(username[i])&&pass.equals(password[i])){//比对\x0d\x0aresponse.getWriter().print("you are welcome!")\x0d\x0aj++\x0d\x0a}else if(user.equals(username[i])&&!pass.equals(password[i])){\x0d\x0aresponse.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n")\x0d\x0aresponse.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right")\x0d\x0aj++\x0d\x0a}else{\x0d\x0acontinue\x0d\x0a}\x0d\x0ai++\x0d\x0a}\x0d\x0aif(j == 0){\x0d\x0aresponse.getWriter().println("Your username may not be properly")\x0d\x0a}\x0d\x0a} catch (Exception e) {\x0d\x0ae.printStackTrace()\x0d\x0a}finally{\x0d\x0aJDBCTools1.release(rs, ps, conn)\x0d\x0a}\x0d\x0a//这是我JDBCTools的getConnection方法\x0d\x0agetConnection{\x0d\x0aString driverClass = oracle.jdbc.driver.OracleDriver\x0d\x0aString jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl\x0d\x0a//你的数据库的用户名密码\x0d\x0aString user = null\x0d\x0aString password = null\x0d\x0a// 通过反射创建Driver对象\x0d\x0aClass.forName(driverClass)\x0d\x0areturn DriverManager.getConnection(jdbcUrl, user, password)}\x0d\x0a//这是我JDBCTools的release方法\x0d\x0apublic static void release(ResultSet rs, Statement statement,\x0d\x0aConnection conn) {\x0d\x0aif (rs != null) {\x0d\x0atry {\x0d\x0ars.close()\x0d\x0a} catch (SQLException e) {\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (statement != null) {\x0d\x0atry {\x0d\x0astatement.close()\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (conn != null) {\x0d\x0atry {\x0d\x0aconn.close()\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)