The details of how to connect to a database are detailed earlier, and the following describes adding data to the database.
Note: If you refer to the following code, you need to
Change the package name, database name, database account number, password, and data table (information in the data sheet)
1 PackageCom.ningmeng;2 3 ImportJava.sql.*;4 5 /**6 * 1: Add data to the database7 * @authorBiexiansheng8 *9 */Ten Public classTest01 { One A Public Static voidMain (string[] args) { - Try { -Class.forName ("Com.mysql.jdbc.Driver");//Load Database driver theSYSTEM.OUT.PRINTLN ("Load Database driver succeeded"); -String url= "Jdbc:mysql://localhost:3306/test";//declaring the URL of the database test -String user= "root";//user name of the database -String password= "123456";//password for the database + //Establish database connection, get Connection object Conn (throw exception) -Connection conn=drivermanager.getconnection (URL, user, password); +SYSTEM.OUT.PRINTLN ("Connection database succeeded"); A //generate a MySQL statement atString sql= "INSERT into users (username,password,age,sex) values (' Heart grow fonder ', ' 123456 ', 22,0)"; -Statement stmt=conn.createstatement ();//Create a statement object -Stmt.executeupdate (SQL);//Execute SQL statement -System.out.println ("Insert to database succeeded"); - conn.close (); -SYSTEM.OUT.PRINTLN ("Close Database Success"); in}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block to e.printstacktrace (); +}// - Catch(SQLException e) { the //TODO auto-generated Catch block * e.printstacktrace (); $ }Panax Notoginseng - } the +}
Detailed Run Results
This can be perfect to insert data, add and delete changes to the first step to perfect solution.
A brief introduction to the knowledge points used:
Once a database connection is established in a Java program, you can use the Createstatement () method of the connection interface to obtain the statement object
You can then add data to the database by executing the SQL statement using the Excuteupdate () method.
The 1:createstatement () method is a method of the connection interface that is used to create statement objects
The 2:connection interface represents a connection to a particular database, and to manipulate the data in the data table in the database, the first thing to do is to obtain a database connection.
The 3:statement interface is used to create an object that passes SQL statements to the database, which provides methods to implement common operations on the database.
The Excuteupdate () method in the 4:statement interface executes the given SQL statement, which can be a insert,update,delete statement.
The second method of
Inserting data into the MySQL database using the PreparedStatement interface
1 PackageCom.ningmeng;2 3 ImportJava.sql.*;4 5 /**6 * 1: Use the PreparedStatement interface to execute the INSERT statement7 * 8 * @authorBiexiansheng9 *Ten */ One Public classTest02 { A - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub the Try { -Class.forName ("Com.mysql.jdbc.Driver");//Load Database driver -SYSTEM.OUT.PRINTLN ("Load Database driver succeeded"); -String url= "Jdbc:mysql://localhost:3306/test";//declaring the URL of the database test +String user= "root";//Database user name -String password= "123456";//Database Password + //establish a database connection and get the connection object Conn AConnection conn=drivermanager.getconnection (URL, user, password); atSYSTEM.OUT.PRINTLN ("Connection database driver succeeded"); - //generate an SQL statement -String sql= "INSERT into users (Username,password,age,sex) VALUES (?,?,?,?)"; -PreparedStatement ps=conn.preparestatement (SQL);//Create a statement object -Ps.setnstring (1, "Lisi");//assign a value to the first question mark in an SQL statement -Ps.setstring (2, "123456");//assign a value to the second question mark in an SQL statement inPs.setint (3,24);//assign a value to the third question mark of the SQL statement -Ps.setint (4,2);//assign a value to the fourth question mark of the SQL statement toPs.executeupdate ();//Execute SQL statement +Conn.close ();//Close the database connection object -SYSTEM.OUT.PRINTLN ("Close Database Connection object"); the}Catch(ClassNotFoundException e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng}Catch(SQLException e) { - //TODO auto-generated Catch block the e.printstacktrace (); + } A the + } - $}
Because I have just accidentally executed the first program more than once, so there is a line of id==7, I hereby comment
The 1:preparedstatement interface inherits statement, executes dynamic SQL statements, executes SQL statements through PreparedStatement instances, and is precompiled and saved to the PreparedStatement instance. The SQL statement can then be executed repeatedly.
2:preparementstatement the method in the interface, such as executeupdate executes the SQL statement in this Preparestatement object, the SQL statement must be a insert.update,delete statement, Or a DDL statement that has no return value.
3:setstring (int pindex,string str) sets the parameter Pindex position to the given String type argument, the saying is that the data type is written in the first place
Setint (int pindex,int x)
All the others are similar, without much narration
Java adds data to the database in Eclipse