What are the steps to access a database in Java? __c#

Source: Internet
Author: User
First, we'll introduce a few related classes
1.DriverManager class: Used to establish a connection to the databaseConnection con = drivermanager.getconnection (URL, "user", "password");
2.Connection InterfaceThere are two main methods:
Statement createstatement ();  Create a statement object and return the specific SQL instruction void close (); Close database connections, freeing resources
3.Statement InterfaceResultSet executequery (String sql);  Executes the SQL command, returning a collection of results, typically used to execute the SELECT query command int executeupdate (String sql); Executes the SQL command that returns the number of record bars that succeeded in the operation. Usually used to execute INSERT, UPDATE, or delete commands, that is, add, delete, and change commands.
4.PreparedStatement InterfaceIt is a subclass of statement, has the ability to precompile SQL commands, and can improve execution efficiency for SQL statements that need to be executed more than once. Generally I am used to do the input parameters of the precompiled SQL statement.
5.ResultSet InterfaceResultSet represents the database result set. The ResultSet object has a pointer to its current data row. The next () method moves the pointer to the next line.
Here are the steps: 1. Registered driver/Load driver
Class.forName ("Com.mysql.jdbc.Driver"); Different databases need to load different parameters, where the MySQL database is loaded with the driver
2, establish the connection
Connection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/databasename", "root", "root");
3. Create Statement/preparedstatement
Statement st = Con.createstatement ();
4. Execute SQL statement
Result set = St.executequery ("SELECT * from user");  Performs a query operation that returns a result int rst = St.executeupdate ("..."); Perform increment, delete, change operation, return the number of rows executed
5. Processing result setThis situation occurs in the execution of the SQL statement is a query statement, return is a result set, generally use the while statement to judge the time with the next line of data, use the next () method to remove the data.
6, close the connection, release the resources
Con.close ();   Close an instance of the connection class

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.