MySQL database usage process, code explanation, mysql usage process
Database usage process:
1. Registration driver;
2. Use DriverManager. getConnection to obtain the connection object con;
Method:
3. Use the createStatement () method of the connection object to obtain the object stmt that can execute SQL statements;
4. stmt executes the query (query statement );
Method B (recommended ):
3. Use the prepareStatement method of the connected object to obtain the pre-processing object pstmt with parameters (query statement;
4. pstmt executes the query and assigns the result set to the ResultSet object;
1 public class DbTest {2 public static void main (String [] args) {3/** 4 * database usage: 5*1. register the driver; 6*2. use DriverManager. getConnection method to obtain the connection object con; 7 * A method: 8*3. use the createStatement () method of the connection object to obtain the object stmt that can execute the SQL statement; 9*4. stmt executes the query (query statement); 10 * B method (recommended): 11*3. use the prepareStatement method of the connected object to obtain the preprocessing object pstmt with parameters (query statement); 12*4. pstmt executes the query and assigns the result set to the ResultSet object; 13 */14 try {15 Class. forName ("com. mysql. jdbc. driver "); // register the Driver 16 Connection con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/book", "root", "root123 ");
// Obtain the connection object 17 // use the statement method to process SQL statement execution, which is slightly different from the PreparedStatement method. 18 Statement stmt = con. createStatement (); // get the object 19 ResultSet rs1_stmt.exe cuteQuery ("select * from bookinfo where id = 1") that can execute the SQL statement; // execute the query, and assign the result set to the ResultSet object 20/**. The following method is recommended: 21 PreparedStatement pstmt = con. prepareStatement ("select * from bookinfo where id = 1"); // obtain the pre-processing object 22 ResultSet rs1_pstmt.exe cuteQuery (); // execute the query, and assign the result set to the ResultSet object 23 */24 while (rs. next () 25 {int id = rs. getInt (1); 26 System. out. println (rs. getString (2) + "" + rs. getString (3) + "" + rs. getString (4); 27} 28} catch (ClassNotFoundException e) {// The capture driver class cannot find exception 29 e. printStackTrace (); 30} catch (SQLException e) {// capture SQL exception 31 e. printStackTrace (); 32} 33} 34}
Help me explain the Php code for connecting to Mysql
<? Php
$ Mysql_server_name = 'localhost'; // change it to your mysql database server.
$ Mysql_username = 'root'; // change the username of your mysql database.
$ Mysql_password = '000000'; // change the password of your mysql database.
$ Mysql_database = 'mydb'; // change the name of your mysql database.
$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password, $ mysql_database); // explain down from the beginning of this sentence
$ SQL = 'insert into book (name, pwd) values ("ggg", "ggg ");';
// This is an SQL statement: Insert a record into the book table
Mysql_query ($ SQL );
// Execute the SQL statement
Mysql_select_db ($ mysql_database, $ conn); // select the database where the above table is located (this statement should be executed before the above sentence)
$ Result = mysql_query ($ SQL); // This sentence is completely redundant. It is the same as the one above!
Mysql_close ($ conn); // close the database connection
Echo "Hello! Operation successful! "; // Display prompt information
?>
To query data in the MySQL database, it is better to have a comment on how to use PHP to compile the query code.
<? Php
$ Db = mysql_connect ("database IP Address", "database username", "Database Password ");
// Example: $ db = mysql_connect ("127.0.0.1", "root", "123456 ");
Mysql_select_db ("Name of the database to be connected", $ db );
$ SQL = "select * from tabel"; // query statement
$ Result = mysql_query ($ SQL );
$ ResultArr = mysql_fetch_array ($ result); returns an array.
Print_r ($ resultArr); print the result
?>