Java uses JDBC to manipulate MySQL database (i)

Source: Internet
Author: User

This example is the basic method for Java to operate MySQL.

Before this example, install MySQL, configure the account password, create a logininfo database, and create a userinfo data table in the database. and add sample data to the table.

First, copy the Mysql-connector-java-5.1.26-bin.jar development package into the Lib folder, and build Path->add build Path.

Second, create the user class, which encapsulates the data read from the database.

1  Packagecom.mylx.database;2 3  Public classUser {4 String ID;5 String username;6 String password;7 8      PublicString getId () {9         returnID;Ten     } One  A      Public voidsetId (String id) { -          This. ID =ID; -     } the  -      PublicString GetUserName () { -         returnusername; -     } +  -      Public voidSetusername (String username) { +          This. Username =username; A     } at  -      PublicString GetPassword () { -         returnpassword; -     } -  -      Public voidSetPassword (String password) { in          This. Password =password; -     } to}
View Source

Create the test class Mydatabasemysql, add the code to connect to the database, execute the query, encapsulate the data, and traverse the data collection in the main function.

1  Packagecom.mylx.database;2 //Java uses JDBC to manipulate MySQL database (i)3 4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 Importjava.sql.Statement;9 Importjava.util.ArrayList;Ten Importjava.util.List; One  A  Public classMydatabasemysql { -  -      Public Static voidMain (string[] args) { theString driverstr = "Com.mysql.jdbc.Driver"; -String dataurlstr = "Jdbc:mysql://192.168.2.100:3306/logininfo"; -String nameStr = "root"; -String passwordstr = "admin"; +String sqlstr = "SELECT * from UserInfo"; -Connection conn =NULL; +Statement stat =NULL; AList<user> userslist =NewArraylist<user>(); at          -         Try { -             //Copy the Mysql-connector-java-5.1.26-bin.jar development package to the Lib folder first -             //and build Path->add build Path -             //Load Driver class - Class.forName (DRIVERSTR); in             //Create a data connection -conn =drivermanager.getconnection (Dataurlstr, NAMESTR, passwordstr); to             //get query data recordset +Stat =conn.createstatement (); -ResultSet rs =stat.executequery (SQLSTR); the             //iterate through the recordset and encapsulate each field data read into a data collection *              for(Rs.first ();!rs.isafterlast (); Rs.next ()) { $User Tmpuser =NewUser ();Panax NotoginsengTmpuser.setid (rs.getstring (1)); -Tmpuser.setusername (Rs.getstring (2)); theTmpuser.setpassword (Rs.getstring (3)); + Userslist.add (tmpuser); A             } the}Catch(Exception e) { + e.printstacktrace (); -}finally { $             if(Conn! =NULL) { $                 Try { -                     //Close Connection - conn.close (); the}Catch(SQLException e) { - e.printstacktrace ();Wuyi                 } the             } -         } Wu         //Traverse DataSet Merge PrintOut -         if(Userslist! =NULL) { About              for(inti = 0; I < userslist.size (); i++) { $System.out.print (Userslist.get (i). GetId () + "--"); -System.out.print (Userslist.get (i). GetUserName () + "--"); -System.out.print (Userslist.get (i). GetPassword () + "\ n"); -             } A         } +     } the}
View Source

Operation Result:

Java uses JDBC to manipulate MySQL database (i)

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.