In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve mysql database programming, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Create a Database object first.
2. Create a Conection object.
Establish a connection to the database (equivalent to opening a mysql client, entering a password, and connecting successfully).
3. Assemble SQL statements.
Use the prepare,Statement object (the client enters a SQL statement).
4. Execute SQL statement.
5. View the results returned by the server (the client displays the results).
6. Close the connection, release resources, and then release (exit the client) first.
Example
Package jar1024; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import javax.sql.DataSource;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException; / / JDBC inserts data public class TestJDBC {public static void main (String [] args) throws SQLException {/ / 1. First create a DataSource object (the life cycle of the DataSource object should follow the whole program) DataSource dataSource = new MysqlDataSource () / / next, configure datasource so that you can successfully access the database / / the main configuration information. URL,User,Password needs to transform downwards ((MysqlDataSource) dataSource) .setURL ("jdbc:mysql://127.0.0.1:3306/jar1024?characterEncoding=utf-8&useSSL=true"); ((MysqlDataSource) dataSource) .setUser ("root") ((MysqlDataSource) dataSource) .setPassword ("123456"); / / 2. Create a Connection object and establish a connection with the database to transfer data to the database. / / Connection package is java.sql / / if it is not normal, it will throw a SQLexception exception / / connection life cycle is short, each request can create a new connection Connection connection = dataSource.getConnection (); / / 3. Assemble sql statements using prepare,Statement objects int id = 1; String name = "cao"; int classId = 10; / /? Is a placeholder that can replace the value of a specific variable to? Position String sql = "insert into student values (?)"; PreparedStatement statement = connection.prepareStatement (sql); / / 1Jing 2Jing 3 is equivalent to? Subscript statement.setInt (1); statement.setString (2); statement.setInt (3); System.out.println ("statement" + statement); / / 4. After assembling, execute SQL / / insert delete update using the executeUpdate method to execute / / select use executeQuery to execute / / return value indicates how many lines int ret = statement.executeUpdate (); System.out.println ("ret" + ret); / / 5. After execution, close and release related resources / / must be created after the first release, pay attention to the order statement.close (); connection.close ();}} above is all the content of the article "how to implement mysql database programming", thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.