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/03 Report--
What are the basic knowledge points of JDBC in java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
As one of the most basic and important javaAPI, the significance of jdbc is self-evident. As a beginner of the java language, it is inevitable to deal with the database, and the simplest tool to operate the database can be said to be jdbc. And if the coders want to learn more advanced database operation techniques such as orm framework such as hibernate or mybatis, they can get twice the result with half the effort. For the purpose of making it convenient for yourself to help others, the basic knowledge points of jdbc found on the Internet are sorted out as follows
Jdbc programming steps: register and load a Drvier driver: Class.forName ("oracle.jdbc.driver.OracleDriver"); create a database connection String url= "jdbc:oracle:thin:@172.16.0.6:1521:tangjl"; String user= "openlab"; String password= "open123"; Connection Con=DriverManager.getConnection (url,user,password)
Connection connection is obtained through the static method getConnection method of DriverManager, which essentially passes the parameters to the connect () method in the actual Dirver to obtain the database connection.
Jdbc:oracle:this: (protocol) @ xxx.xxx.xxx:xxx (ip address and port number): xxx (using database name)
Mysql's url is jdbc:mysql://localhost:3306/test.
Get a statement object (PreparedStatement) String sql= "select * from a_yyy"; Statement s=con.createStatment (); PreparedStatement ps=con.prepareStatement (sql); execute the sql statement through Statement:
The Statement interface represents the state of a database. When sending the corresponding SQL statement to the database, you need to create a Statement interface or a PreparedStatement interface. In specific applications, Statement is mainly used to operate SQL statements without parameters (which can be run directly), such as delete statements, add or update.
Sta.execute (): suitable for any statement
Sta.executeQuery (sql): suitable for query statements. Returns a query result set.
Sta.executeUpdate (sql); suitable for adding, deleting, modifying and creating statements. The returned value is the number of entries that affect the record.
/ / /
PreparedStatement: precompiled Statement.
If PreparedStatement: after getting the ps:
Similarly, if you add, delete, change, create, use ps.executeUpdate (); if you query, use ps.executeQuery () to return a result set.
Precompiled PreparedStatement is more efficient and improves performance for executing isomorphic sql statements.
Step 1: get the PreparedStatement object through the connection, using the placeholder (?) The construction of the sql statement of the
PreparedStatement pstm = con.preparedStatement ("select * from test where id=?")
Step 2: set parameters
Pstm.setString (1, "ganbin")
Pstm.setDate (2 perfect date)
Step 3: execute the sql statement
Rs = pstm.excuteQuery ()
When statement sends a complete Sql statement to the database, it is not executed directly but is compiled and then run by the database, while PreparedStatement first sends the Sql statement with parameters, and then sends a set of parameter values. If it is a homogeneous sql statement, PreparedStatement is more efficient than statement. For heterogeneous sql, the efficiency of the two is about the same.
Isomorphism: the compilable parts of two Sql statements are the same, only the parameter values are different.
Heterogeneity: the format of the entire sql statement is different
Note: 1. Use precompiled Statement to compile multiple Sql statements and execute them at once.
2. it can be used across databases to write general programs.
3. Use precompilation as much as possible when you can
Process the query result set:
Only if the select statement is executed will there be a result set
6. Close the data source
Close details: get it first and close it last.
Metadata: information about data, such as type or capacity, which can be accessed through JDBC API:
1 Database metadata:
Use the Connection.getMetadata method to return a DataBaseMetaData reference.
2 result set metadata
A use the ResultSet.getMetadata method to return a ResultSetMetaData reference
B can use getColumnCount and other class methods to obtain result set information. (check API)
The three steps of transaction processing:
① connection.setAutoCommit (false); / / turn off autocommit
② normal DB operation / / if a SQL statement fails, it will be rolled back automatically
③ connection.commit () / / active submission or connection.rollback () / / active rollback
Complete code:
Try {con.setAutoCommit (false); / / step1 turn off autocommit Statement stm = con.createStatement (); stm.executeUpdate ("insert into person (id, name, age) values (520,18)"); stm.executeUpdate ("insert into Person (id, name, age) values (521, 'Super', 19)"); / / step2 normal DB operation con.commit () / / step3 successfully submitted} catch (SQLException e) {con.rollback (); / / if step3 failed, the metadata was actively rolled back:
JDBC uses metadata (MetaData) to get specific table-related information, and can query which tables are in the database, which fields are in the table, and the properties of the fields. Meta data returns this information to us through a series of getXXX.
Database metadata DatabaseMetaData is obtained by using connection.getMetaData ()
MetaData includes: contains metadata information about the database as a whole.
The result set metadata ResultSetMetaData is obtained using resultSet.getMetaData ()
It is important to obtain the column name, number of columns and other information of the table.
Result set metadata object: ResultSetMetaData meta = rs.getMetaData ()
Number of fields: meta.getColomnCount ()
Field name: meta.getColumnName ()
Field JDBC type: meta.getColumnType ()
Field database type: meta.getColumnTypeName ()
Database metadata object: DatabaseMetaData dbmd = con.getMetaData ()
Database name = dbmd.getDatabaseProductName ()
Database version number = dbmd.getDatabaseProductVersion ()
Database driver name = dbmd.getDriverName ()
Database driver version number = dbmd.getDriverVersion ()
Database Url=dbmd.getURL ()
Login name of the connection = dbmd.getUserName ()
What are the basic knowledge points of JDBC in java? the answers to the questions are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.