Complete student management system with reflection (including database connection)

Source: Internet
Author: User
Tags object object

today, I finally put the pit Dad's universal database link to do the first step, insert data. Although very simple, but still should summarize some questions. All the frustrations can only be expressed now. Here are some questions and procedures: the most important problem may be the SQL statement, which today has been a long time in the SQL statement errors.
first, we need to summarize some of our SQL statements:
To determine whether a table exists:Method:ResultSet gettables (String catalog,String Schemapattern,String Tablenamepattern,string[] types)
Parameters:Catalog-The category name, because it is stored in the database, so it must match the category name. The parameter is "" to retrieve a description without a category, or 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, so it must match the schema name. The parameter is "" to retrieve those descriptions that do not have a schema, or null to indicate that the schema name should not be used to narrow the search
Tablenamepattern-table name pattern, 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 row is a table description
Here's the code:DatabaseMetaData meta = con.getmetadata ();ResultSet rstable = meta.gettables (null, NULL, this.) Clazz.getsimplename (), null), if (Rstable.next ()) {table exists}

After judging the table, we should do the next step: if no table, new, add Data: New Table statement: Creat table tablename (ID varchar (255), age int (255)) After the argument is: Your variable name variable type (variable length) , ......
Statement to add data: INSERT INTO TableName (ID, age) values here is an unknown solution.
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";//mysql user name 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:As for the final execution of the SQL statement, it is easy to confuse statement with the three execute methods under this interface. The difference 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))//Determine data type {value = valuestring; }...... There's a lot of other types of judgment, and I'm not going to stick around here.
Each time the input is completed, the object information is saved Field.set (object, value); Finally, save the data to the global variable so that it is finally ready to be passed to the database 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); }
Here, the basic is done. Insert in database and CREATE table also directly on the code does not go to the surface of the method everyone open one eye to close one 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 student management system with reflection (including 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.