Home > Back-end >  JDBC
JDBC

Time:09-26

1, in the development environment of the specified database driver, for example, the next experiment, using the database is MySQL, so need to download MySQL support JDBC driver (the latest is: MySQL connector - Java - 5.1.18 - bin. Jar); And the development environment is MyEclipse, will download to get the driver is loaded into the development environment of (concrete examples will explain how to load),

2, the load Driver in the Java program, in a Java program, can be "class.forname (" specify the database Driver") "way to load the add to the Driver in the development environment, such as loading of MySQL data Driver code for: class.forname (". Com. MySQL. JDBC Driver")

3, create a data Connection object: through the DriverManager class to create a database Connection object Connection, DriverManager class action between Java program and the JDBC driver, used to check whether the driver loaded by the Connection is established, and then through the getConnection method, based on the database URL, user name and password, create a JDBC Connection object, such as: Connection Connection=DriverManager. GeiConnection (" connect to the database URL ", "user name", "password"), among them, the URL=protocol name + port IP address (domain name) + + the name of the database; Refers to login user name and password when the database is used by the user name and password, concrete examples create MySQL database connection code is as follows:

The Connection connectMySQL=DriverManager. GeiConnection (" JDBC: mysql://localhost: 3306/myuser ", "root", "root");

4, create a Statement object: the Statement class is mainly used to perform static SQL Statement and returns the objects it generate results, through the Connection object createStatement () method can create a Statement object, for example: the Statement statament=Connection. The createStatement (); Concrete example to create a Statement object code is as follows:

The Statement statamentMySQL=connectMySQL. CreateStatement ();

5, call the relevant Statement object method performs the corresponding SQL Statement: through execuUpdate () method is used to update data, including insert, and delete operations, such as the staff table to insert a data code:

Statement. ExcuteUpdate (" INSERT INTO the staff (name, age, sex, address, depart, worklen, wage) "+" VALUES (' Tom1 '321,' M ', 'China' and 'Personnel', '3', '3000') ");

Object by calling the Statement executeQuery () method for data query, the query results will be ResulSet object, ResulSet said after the execution of querying the database returns a collection of data, ResulSet object has a pointer can point to the current data line, through the object's next () method, which makes the pointer to the next line, and then the data in column number or the field name, if the next () method returns null, said the next line, no data exist in using the sample code is as follows:

The ResultSet resultSel=statement. ExecuteQuery (" select * from staff ");

6, close the database Connection: use or do not need to access the database, the database through the Connection of the close () method of closing data Connection in time,

CodePudding user response:

And then what are you trying to say?

CodePudding user response:

The author did not ask what question, however, hurry to back to learn, to learn the mybatis - plus, or spring encapsulate JDBC will wonder, who I was, I at that time

CodePudding user response:

1. The first MySQL driver is wrong, latest
2. If it is the latest, the driver is complains you use the
3. May need to join the time zone, coding
4. Finally these don't appear in the BBS, you only need to understand the code running process and writing can,
Use less than 5. After the code