Home > Back-end >  JDBC query common beginner can practice logic
JDBC query common beginner can practice logic

Time:12-09

 
Static int l;

The public & lt; T> List GetAll (String SQL, T classS, Object... Objects)
Throws NoSuchFieldException SecurityException, IllegalArgumentException, IllegalAccessException {
List ListNews_detail=null;
L=objects. Length;//the Object... Objects in the form of an array length return
Object [] params=new Object [l];//instantiate the length of the array is L
for (int i=0; I & lt; Objects. Length; I++) {
Params [I]=objects [I];//instantiate an array of assignment after
+ + l;
}
The ResultSet rs=QueryDataBase (SQL, params);//call the method
Try {
Class<? Extends Object> Newclass=classS. GetClass ();//reflection classS
The Object newInstance=null;
ListNews_detail=new ArrayList<> (a);//define an array of
Class<?> Class.forname=Class. Class.forname (Newclass getName ());//reflection
The Field [] declaredFields=class.forname. GetDeclaredFields ();//reflecting classS class attribute to an array of
String columnName=null;
While (rs), next ()) {//a pointer pointing to the line
NewInstance=class.forname. NewInstance ();//call the structure data type and database data types as
For (int j=0; J & lt; DeclaredFields. Length; J++) {//declaredFields length attribute number
//declaredFields [j] getName () an array name -- through class.forname. GetDeclaredField return property (not attribute string)
Field declaredField=class.forname. GetDeclaredField (declaredFields [j]. J getName ());
Class<?> Type=(Class) DeclaredFields [j]. GetType ();//declaredFields [j]. GetType () an array type
String name=declaredFields [j] getName ();//declaredFields [j] getName () an array name
DeclaredField. SetAccessible (true);//will be able to access private methods,
String newName=name. The substring (0, 1). The toUpperCase () + name. The substring (1);//string interception and the first letters capitalized
Method Method=class.forname. GetMethod (" set "+ newName, type);//class.forname. GetMethod -- --
call reflected in the class method//- type is the type (conversion)
//String columnName=rs. For getMetaData (). The getColumnName ()//show the number of SQL statements inside *;
Int columnCount=rs. For getMetaData (.) getColumnCount ();//field length
If ((j + 1) & lt;=columnCount) {//control getColumnLabel (j + 1); The length of the bracket inside
ColumnName=rs. For getMetaData (.) getColumnLabel (j + 1);//for getMetaData () to obtain table - getColumnLabel (j +
//1) get the name of the table columns
}
//System. Out. Println (columnName) equals (name));
//System. Out. Println (columnName + rs. For getMetaData () getColumnCount ());
If (type==Integer. Class) {//compare type is equal to the change of type
If (columnName. Equals (name)) {//to determine whether a table column there
Method. The invoke (newInstance, rs. Get int (name));//using the method called assignment (class, assignment)
}
} else if (type==Object. The class) {
{if (columnName. Equals (name))
Method. The invoke (newInstance, rs. GetObject (name));
}
} else if (type==String class) {
{if (columnName. Equals (name))
Method. The invoke (newInstance, rs. Get string (name));
}
} else if (type==Double class) {
{if (columnName. Equals (name))
Method. The invoke (newInstance, rs. GetDouble (name));
}
} else if (type==Date. Class) {
{if (columnName. Equals (name))
Method. The invoke (newInstance, rs. GetDate (name));
}
} else if (type==Short. Class) {
{if (columnName. Equals (name))
Method. The invoke (newInstance, rs. GetShort (name));
}
}
}

ListNews_detail. Add (newInstance (T));//add collection
}
Return listNews_detail;
} the catch (SQLException e) {
e.printStackTrace();
} the catch (NoSuchMethodException e) {
e.printStackTrace();
} the finally {
Close (rs);
Return listNews_detail;
}
}



 

String SQL="SELECT id, categoryId, summary the FROM news_detail WHERE the summary=? ";
BestDao dao=new BestDao ();
News_detail detail=new News_detail ();
ListFor (the Object Object: all) {
System. The out. Println (object);
}




CodePudding user response:

If you see this is for new entrants, you named this style should be some specification,
Moreover, the couple look not to understand, the old man didn't want to see
  • Related