In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of JDBC connecting MySQL and realizing fuzzy query". The content of the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "what is the method of JDBC connecting MySQL and realizing fuzzy query".
Scene:
In learning the JDBC language, you perform a few common steps each time: register the driver, get the connection, create the operation, process the results, and release resources are too complex, so you might as well package the above steps into a tool class and provide only external methods!
Description:
This is the code written without the encapsulation of the tool class, which is redundant and complex.
Package com.zdx.JDBC; import java.sql.*; public class JAVA1129_5 {public static void main (String [] args) {/ / set empty objects, register drivers, get connections, create operations, process result sets, release resources String url = "jdbc:mysql://127.0.0.1:3306/hello"; String username = "root"; String password = "rota" String SQL = "insert into stu values" (1), (2) "update stu set sname" update stu set sname = 'xzq',major='bask' where sno =' 1century; "; String SQL1=" select * from stu "; Connection connection = null; Statement statement = null; ResultSet resultset = null Try {Class.forName ("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection (url, username, password); statement = connection.createStatement (); int cnt = statement.executeUpdate (SQL); if (cnt! = 0) {System.out.println ("execution successful") } ResultSet result = statement.executeQuery (SQL1); while (result.next ()) {/ / manipulate the Operand System.out.println ("nbnb") as the cursor moves;}} catch (SQLException e) {e.printStackTrace () } catch (ClassNotFoundException e) {e.printStackTrace ();} / / release resources. You must add a block of finally statements at the end to execute finally {if (resultset! = null) {try {resultset.close () } catch (SQLException throwables) {throwables.printStackTrace ();}} if (statement! = null) {try {statement.close () } catch (SQLException throwables) {throwables.printStackTrace ();}} if (connection! = null) {try {connection.close () } catch (SQLException throwables) {throwables.printStackTrace ();}} solution:
First, the constructor in the class adds a private modifier to mimic the Sun utility class, such as the Arrays class and Collection.
Secondly, register the driver, using the static code block to register the driver only once.
Then get the database connection, and the method that returns the database connection object has an exception and cannot catch, so it needs to be thrown out.
Finally, a closed method is encapsulated.
Note that because the utility class is encapsulated and only methods are provided externally, they are encapsulated into class methods (that is, static modification).
Package com.zdx.JDBC; import java.sql.*; / / 2021.11.2903 encapsulates the utility class of the database public class DBUtil {private DBUtil () {} static {try {Class.forName ("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace () }} / / use static code blocks to register the driver with features that load only once when the class is loaded. / / the method public static Connection getConnection (String url,String user,String password) throws SQLException {return DriverManager.getConnection (url,user,password); / / Note here that the return object of the get connection method called in the driver management class is the connection object. } / / close resources / / in order, result set, database operation object, connection object! Public static void close (Connection connection,Statement ps,ResultSet resultSet) {if (resultSettings null) {try {resultSet.close ();} catch (SQLException throwables) {throwables.printStackTrace ();}} if (psometric settings null) {try {ps.close () } catch (SQLException throwables) {throwables.printStackTrace ();}} if (connectionless null) {try {connection.close ();} catch (SQLException throwables) {throwables.printStackTrace ();}
Call the tool class to implement the fuzzy query:
Package com.zdx.JDBC; import java.sql.*; public class Main {public static void main (String [] args) {Connection connection = null; PreparedStatement ps = null; ResultSet resultSet = null; String url = "jdbc:mysql://127.0.0.1:3306/hello"; String user = "root"; String password = "rota" / / get the connection try {connection = DBUtil.getConnection (url, user, password);} catch (SQLException throwables) {throwables.printStackTrace ();} / / get the precompiled database operation object String SQL = "select sname from stu where sname like?"; try {ps = connection.prepareStatement (SQL) Ps.setString (1, "_ y%"); resultSet = ps.executeQuery (); while (resultSet.next ()) {System.out.println (resultSet.getString ("sname"));} catch (SQLException throwables) {throwables.printStackTrace () } / / release resource finally {DBUtil.close (connection, ps, resultSet) Thank you for your reading, the above is the content of "what is the method of JDBC connecting MySQL and realizing fuzzy query". After the study of this article, I believe you have a deeper understanding of what is the method of JDBC connecting MySQL and realizing fuzzy query, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.