I have been learning java for nearly ten weeks. Finally learned the SQL statement. Compared with the language of java, SQL statements are too simple to be ridiculous. But is such a simple language, the most prone to compile errors. Because we are writing SQL statements in the java compiler, it is easy to have a situation where the statement is correct but the program does not run well. Let me introduce it better! Linking the database is not difficult, as long as you pass the following statements.
//Load the driver
Class.forName("com.mysql.jdbc.Driver");
//Establish a connection, localhost means the machine, atm means the database library name, 3306 is the port when installing the database
Connection conn =DriverManager.getConnection(" jdbc:mysql://localhost:3306/atm","root","root");
//Create sql statement transfer object
Statement stmt = conn.createStatement();
//ResultSet pointer positioning statement
ResultSet rs = stmt.executeQuery ("");
//The function that can edit the SQL statement
stmt.execute("SQL statement");
The above five sentences are the most basic sentences. Through the above five sentences, it is enough to simply read and write the data in the database , delete, modify the operation, and can link the database.
Here are a few mistakes I made,
using the pointer to locate the statement
rs = stmt.executeQuery("
After that, we need to operate on the line that rs points to, and add a judgment statement
if (rs.next())
to judge whether there is a line of data with code = 1001. It can also make the function of rs execute. (Without this judgment statement, there will be an error at runtime.)
When using
stmt.execute("SQL statement"); you should pay
more attention to the specification. A space or a missing double quotation mark will make your program unable to run.
Taking my experience today, the following two statements are the most representative:
stmt.execute("update account set password="+a+"where code="+param[1]); //Error at runtime
stmt.execute( "update account set password="+a+" where code="+param[1]);//It works
fine The difference between these two statements is just spaces.
From a mistake, we really should be standard when writing statements. . .
About SQL statements.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326988247&siteId=291194637
Recommended
Ranking