SQL 쿼리를 완료하고 쿼리 결과를 벡터 컨테이너에 넣어 다른 프로그램을 사용할 수 있습니다.
/** SQL 쿼리 명령문*/public static <t> vector <t> executeQuery (class <t> Clazz, String SQL, Object ... Args) {Connection Conn = NULL; PreparedStatement PreparedStatement = NULL; rs = NULL; VECTOR <T> VECRS = NEW VECTOR <T> (); T OBJ = NULL; CONN = {CONN = {CONN = {CONN = QULL; jdbctools.getConnection (); prepartatement = conn.preparestatement (sql); // sql 문을 사용하여 (int i = 0; i <args.length; i ++)를 선택하는 열을 결정합니다. {proadestatement.setobject (i+1, args [i]); Class // 결과 Street_id의 별칭을 얻습니다. jdbc의 메타 데이터를 얻습니다. // 결과 세트의 각 열의 값을 얻고 이전 단계를 결합하여 맵을 가져 오기 Key-value 쌍 // 열 별명 값 : 열 값 // 반사를 사용하여 엔티티 클래스의 속성에 값을 할당하십시오. 메타 데이터 resultSetmetadata rsmd = rs.getmetadata (); map <string, object> mapmetadata = new Hashmap <string, object> (); // 열의 열 이름을 인쇄하는 동안 (rs.next ()) {// 요구 사항을 충족시키고 (int i = 0; i <rsmd.getcount); columnLabel = rsmd.getColumnLabel (i + 1); 오브젝트 columnValue = rs.getObject (columnLabel); // system.out.println (columnLabel); mapMetAdata.put (columnLabel, columnValue);} // 반사를 통해 t-type 객체를 초기화 if (mapMetadata.size ()> 0) {obj = clazz.newinstance (); for (map.Entry <string, eTrys : mapMetAdata.EntrySet ()) {string fieldKey = entery.getKKey (); GetSKEY (); System.out.println (FieldKey + ":" + FieldValue); reflectionUtils.setfieldValue (obj, fieldKey, fieldValue); // 반사를 통해 값을 할당}}}}}} // 객체를 벡터 컨테이너 vecrs.add (obj);}} catch (Exception e) {e.printstacktrace ();} return vecrs;}에로드합니다.사용 된 도구와 같은 방법
데이터베이스 연결 가져 오기 jdbctools.getConnection ()
/** 데이터베이스에 연결됩니다*/public static connection getConnection () getConnection ()은 예외 {connection conn = null; string driver = null; string jdbcurl = null; String username = null; String password = null; // 특성 객체 속성 속성 = new 속성 (); inputStream in = = inputStream in = null; jdbctools.class.getClassLoader (). getResourceAsStream ( "jdbc.properties"); properties.load (in); driver = properties.getProperty ( "driver"); jdbcurl = properties.getProperty ( "jdbcurl"); username = getproperty ( "); properties.getProperty ( "password"); class.forname (driver); conn = drivermanager.getConnection (jdbcurl, username, password); return conn;}reflectionUtils.setfieldValue (OBJ, FieldKey, FieldValue);
OBJ 객체의 FieldKey 속성을 FieldValue에 할당
// 객체의 속성 설정 public static void setfieldValue (Object OBJ, String FieldName, Object Value) {Field Field = getDeclaredfield (obj, fieldname); if (field == null) {trans new new OrgalArgumentException ( ""field에서 [ "+ fieldname+"]를 찾을 수 없습니다. [ "+obj+"] ");} makeaccessible (field); try {field.set (obj, value);} catch (delegalaccessexception e) {system.out.println ("던져 질 수없는 예외 ");}}} // 대중의 정적 허가를 바꾸는 권한이 바뀌 었습니다. {field) {if (! modifier.ispublic (field.getModifiers ())) {field.setAccessible (true);}} // 필드 속성을 가져 오면, 속성은 부모 클래스에서 공개 정적 필드를 상속받을 수 있습니다. 공개 정적 필드 getDeclaredfield (class <?> clazz!). clazz = clazz.getSuperClass ()) {try {return clazz.getDeclaredfield (FieldName);} catch (예외 e) {}} return null;}요약
위의 것은 쿼리를 구현하기 위해 SQL 문을 실행하는 Java의 일반적인 방법에 대한 자세한 설명입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!