The first example (the whole process) of Java calling MySql in Linux and various misunderstandings of MySQL password change

Source: Internet
Author: User

Step 1: Install JDK and eclipse, refer to the above: http://blog.csdn.net/yanzi1225627/article/details/7736364

 

Step 2:Download mysql-connector-java-5.1.24.tar.gz, address: http://www.mysql.com/downloads/connector/j/5.1.html

 

Step 3: ExtractMysql-connector-java-5.1.24.tar.gz this file to/usr/local/Java /. In fact, this directory is arbitrary, just for convenience. I installed both JDK and eclipse in the directory above. As follows:

Step 4:Create a new testmysql project, right-click the project --- build path --- add External Archives, and add the. jar file in the above mysql-connector-*** folder.

 

Step 5: Log onto MySQL to create a database yan1, and create the table users in this database. Related commands:

LOGIN command: mysql-u root-P; enter the password

Database creation: Create Database yan1;

Display Library: Show databases;

Create Table users (userid varchar (8), username varchar (8 ));

Insert data: insert into users values ("001", "wenshu ");

Insert into users values ("002", "puxian ");

Show data: Select * from users;

Delete Database: drop database yan1;

 

 

Step 6: write a program in Java. The source code is as follows:

Package Yan. Guoqi. testmysql; import java. SQL. *; public class testmysql {// JDBC driver location private string driver = "com. MySQL. JDBC. Driver ";// This driver is default// Test the location of the database testdb private string url = "JDBC: mysql: // localhost: 3306/yan1 ";// Yan1 is the database name// MySQL user name and password private string user = "root"; private string Password = "123456"; // connection handle private connection conn = NULL; public static void main (string [] ARGs) {// todo auto-generated method stubsystem. out. println ("Hello! "); Testmysql test = new testmysql (); test. test ();}/* Java Connection database */Public void connect () {// load database JDBC driver try {class. forname (driver);} catch (classnotfoundexception e) {e. printstacktrace ();} // use the given database location, user name, and password to connect to the database try {conn = drivermanager. getconnection (URL, user, password); If (! Conn. isclosed () {system. out. println ("succeeded connect to the Database") ;}} catch (sqlexception e) {e. printstacktrace () ;}/ * close connection */Public void close () {try {Conn. close ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}/ * test the database interface function */Public void test () {// connect to connect (); try {// call MySQL to execute a specific SQL statement = Conn. createstatement (); string SQL = "select * from users ";// Note that users is the name of the created table.Resultset rs = statement.exe cutequery (SQL); // traverses the select result while (RS. next () {// Rs. getstring (string) obtains the value of the corresponding column name for a record. system. out. println ("ID:" + Rs. getstring ("userid") + "name:" + Rs. getstring ("username "));
               // Note that the userid and username are consistent with the columns in the created table.} Catch (sqlexception e) {e. printstacktrace () ;}close ();}}

The information is printed in the console!

In fact, we can simplify the program, and there are only four lines of effective code. The simplified code is as follows:

Package Yan. guoqi. testmysql2; import Java. SQL. *; public class testmysql {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub try {class. forname ("com. mySQL. JDBC. driver "); connection Ct = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/yan1", "root", "123456"); statement ST = CT. createstatement (); resultset rs = st.exe cutequery ("select * from users "); While (RS. next () {system. out. println ("ID:" + Rs. getstring (1) + "name:" + Rs. getstring (2) ;}} catch (exception e) {// todo: handle exceptionsystem. out. println ("exception! ");}}}

In addition, it is inconvenient to add the package to the path for each new project. You can copy that mysql-connector-java-5.1.24-bin.jar to the JDK installation directory/JRE/lib/EXT folder on OK, no need to add path.

 

Password Change considerations

Assume that you can log on to the database with password 123456, and change the password to Yanyan.

 

Method 1:In the terminal, note that you have not logged on to the database. Enter the command mysqladmin-p123456 password Yanyan.

A warning is displayed: Warning: using a password on the command line interface can be insecure. This indicates that the change has been successful. Common Mistakes:
Misunderstanding 1: I have logged on to MySQL with my original password. If I change the password, an error is returned. Be sure to change it directly on the terminal before login.

Misunderstanding 2: Write-p123456 as-P 123456. In this case, an error is reported.-P cannot be followed by spaces.

Misunderstanding 3: An "-" is added before the password, and an error is reported.

Note: there must be a space between the password and the new password. The new password can be enclosed. This quotation mark is required when there are special characters.
For more information about password change, see encrypt. According to his statement, enter mysqladmin-u Root Password "XXXX" in the terminal during the First Login, And you can set the password to xxxx. I did not verify this. Note: This is mysqladmin-u root-Password ab12.
This is incorrect, and there cannot be "-" before the password.

 

Method 2: Make changes after logging on to MySQL

Mysql> Update mysql. User SET Password = PASSWORD ('yanyanc') where user = 'root ';
Query OK, 4 rows affected (0.00 Sec)
Rows matched: 4 changed: 4 Warnings: 0

Mysql> flush privileges;
Query OK, 0 rows affected (0.00 Sec)

Misunderstanding: only the first line of command is run. Without flush privileges, the password cannot be changed.. In addition, the commands provided here are capitalized and lowercase, so it is better to make them all lowercase. It has been verified to be completely feasible.

In summary, I prefer the first Password Change method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.