Complete the student management system with reflection (includes database connection)

Source: Internet
Author: User
Tags object object reflection

today I finally put the pit father's Universal database link to do the first step, insert data. Although very easy. However, some questions should be summed up.

The frustrations of the road can only be expressed today. Here are some questions and procedures: the most important problem may be the SQL statement. It's been a long time today. The error in the SQL statement.
first, we need to summarize our SQL several statements:
infer whether the table exists:Method:ResultSet gettables (String catalog,String Schemapattern,String Tablenamepattern,string[] types)
Number of references:Catalog-the category name, which must match the category name, because it is stored in the database.

The reference is "" to retrieve a descriptive narrative without a category. Is null to indicate that the category name should not be used to narrow the search scope
Schemapattern-pattern name mode, because it is stored in the database, it must match the schema name. The reference is "" to retrieve descriptive narratives that do not have a pattern, and Null indicates that the schema name should not be used to narrow the search
Tablenamepattern-table name mode. Because it is stored in the database. So it must match the table name
Types-a list of table types to include, NULL to return all types
Return:
ResultSet-Each line is a table descriptive narrative


Here's the code:DatabaseMetaData meta = con.getmetadata ();ResultSet rstable = meta.gettables (null, NULL, this.) Clazz.getsimplename (), null), if (Rstable.next ()) {table exists}

After inferring the table, we should do the next step: Assume that there is no table, new. Add Data: New Table statement: Creat table tablename (ID varchar (255), age int (255)) The following arguments are: Your variable name variable type (variable length), ...
Add a statement to the data: INSERT INTO TableName (ID, age) VALUES (at) This is not a specific explanation.
Other connection database directly on the code://-----------------------------Connection Database--------------------------------------//Public Connection  Connectingmysql () throws sqlexception{String drivename = "Com.mysql.jdbc.Driver";  String url = "Jdbc:mysql://localhost:3306/dbhouse";  String userName = "root";//mysqlusername string password = "root";//mysql user password try {class.forname (drivename);  } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();  } connection = drivermanager.getconnection (URL, userName, password); return connection; } //---------------------------------------------------------------------------------------//

There is also a noteworthy place:On the last run of the SQL statement, statement this interface under the three with Execute method, very easy to confuse. Differences between execute, ExecuteQuery, and executeupdate in MySQL: http://blog.csdn.net/error/404.html?from=http%3a%2f% 2fblog.csdn.net%2fmon_key_dt%2farticle%2fdetails%2f40516043
II. data on reflection transmissionThe first thing passed in is the name of the object I want to save, and the Newinstance () method to remove my objects Object object = this. Clazz.newinstance ();
This is the variable name and variable type in the Loop output object, prompting for the for Field Field:this. Clazz.getdeclaredfields ()) System.out.println ("Please input" +field.getname () + "(" +field.gettype (). Getsimplename ()    +"):"); String valuestring = Scanner.nextline ();       Input data Object value = NULL;    if (Field.gettype (). Equals (String.class))//Inferred data type {value = valuestring; }...... There's a lot of other types of inference, and I'm not going to stick around here.
After each input, save the object information Field.set (object, value); At last. Save the data to the global variable so that it can be finally passed to the database for dataname + = Field.getname () + ","; Name of all data if (Field.gettype (). Equals (Integer.class) | | field.gettype (). Equals (Int.class)) {data[datalength + +] = fie Ld.getname () + "int (255),"; Prepare the creat table for the database DataType + = "int,";     Data type SYSTEM.OUT.PRINTLN ("datatype:" + datatype); DataValue + = valuestring + ",";    Data value System.out.println ("DataValue:" + datavalue);     } else {data[datalength + +] = Field.getname () + "varchar (255),";     DataType + = "varchar,";     System.out.println ("datatype:" + datatype);     DataValue + = valuestring + ",";    System.out.println ("DataValue:" + datavalue); }
to here. The basic is already complete. Insert in database and CREATE table also directly on the code does not go to the surface of the method everyone open a single eye just close eye.
New Table String tablemessage = "";   for (int i = 0; i < datalength; i++) {tablemessage + = Data[i]; } String sql_newtable = "CREATE TABLE" + this.   Clazz.getsimplename () + "(" + tablemessage.substring (0, Tablemessage.length ()-1) + ")";      System.out.println (sql_newtable);   Statement.execute (sql_newtable); Insert data String Sql_add = "INSERT INTO" + this. Clazz.getsimplename () + "(" + dataname.substring (0, Dataname.length ()-1) + ")"//variable name + "values (" + datavalue.sub String (0, Datavalue.length ()-1) + ")";//(?

,?,?,?

)

System.out.println (Sql_add); Statement.executeupdate (Sql_add);

Small white One, the wrong place hope that everyone in time to help me correct.

Complete the student management system with reflection (includes database connection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.