When adding data to a database through JDBC, INSERT statements are used to implement the insertion of the data, and then the parameters in the SQL statement can be used as placeholders. "To override and then assign a value to the PreparedStatement object or statement object and execute SQL.
The following respectively for the PreparedStatement interface and statement interface Insert data method to do code implementation: wherein the Preparedstatementtest class is used in the PreparedStatement interface method to implement database data insertion In the Statementtest class, database data insertion is implemented using the Excuteupdate (SQL) method in the statement interface.
1 Public classbook{2 //numbering3 Private intID;4 //Book name5 PrivateString name;6 //Price7 Private DoublePrice ;8 //Quantity9 Private intBookcount;Ten //author One PrivateString author; A - Public intgetId () { - returnID; the } - - Public voidSetId (intID) { - This. ID =ID; + } - PublicString GetName () { + returnname; A } at Public voidsetName (String name) { - This. Name =name; - } - Public DoubleGetPrice () { - returnPrice ; - } in Public voidSetprice (DoublePrice ) { - This. Price =Price ; to } + Public intGetbookcount () { - returnBookcount; the } * Public voidSetbookcount (intBookcount) { $ This. Bookcount =Bookcount;Panax Notoginseng } - PublicString Getauthor () { the returnauthor; + } A Public voidSetauthor (String author) { the This. Author =author; + } - } $ /*** * Method one: Inserting data through a method in the PreparedStatement interface * * * **/ $ Public classpreparedstatementtest{ -Book book =NewBook (); - //Load Database driver, register to drive manager theClass.forName ("Com.mysql.jdbc.Driver"); - //Database connection StringWuyiString url = "Jdbc:mysql://localhost:3306/test"; the //Database user name -String username = "root"; Wu //Database connection Password -String Password = "123"; About //Create a connection connection $Connection conn =(Connection) drivermanager.getconnection (Url,username,password); - //SQL statement to insert book information -String sql = "INSERT into booktable (name,price,bookcount,author) value (?,?,?,?);"; - //Get PreparedStatement APreparedStatement PS =conn.preparestatement (SQL); + //Assigning a value to the 1th parameter in an SQL statement thePs.setstring (1, Book.getname ()); - //Assigning a value to the 2nd parameter in an SQL statement $Ps.setdouble (2, Book.getprice ()); the //Assigning a value to the 3rd parameter in an SQL statement thePs.setint (3, Book.getbookcount ()); the //Assigning a value to the 4th parameter in an SQL statement thePs.setstring (4, Book.getauthor ()); - //performs an update operation that returns the number of rows affected in introw =ps.executeupdate (); the //determine if the update was successful the if(Row > 0){ AboutOut.print ("Add Success" +row+ "Bar Data"); the } the //Close PreparedStatement, release resources the ps.close (); + //Close Connection Release resources - conn.close (); the Bayi } the the /*method Two: Inserting data through the executeupdate (SQL) method in the statement interface*/ - Public classstatementtest{ -Book book =NewBook (); the //Load Database driver, register to drive manager theClass.forName ("Com.mysql.jdbc.Driver"); the //Database connection String theString url = "Jdbc:mysql://localhost:3306/test"; - //Database user name theString username = "root"; the //Database connection Password theString Password = "123";94 //Create a connection connection theConnection conn =(Connection) drivermanager.getconnection (Url,username,password); the //Insert a book information SQL statement theString sql= "INSERT into booktable (name,price,bookcount,author) value (" +book.getname () + "," +book.getprice () + "," + Book.getbookcount () + "," +book.getauthor () + ")";98 //Get Statement Interface Object AboutStatement stat =conn.createstatement (); - //Call the Excuteupdate (SQL) method in the statement interface to execute the SQL statement to insert the data101 stat.executeupdate (date);102 103 //Close the statement interface object104 if(Stat! =NULL){ the stat.close ();106 }107 //Close the Connection interface object108 if(Conn! =NULL){109 conn.close (); the }111}
---restore content ends---
Database connected to the JDBC operations database