Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JDBC to realize Database connection in Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Java how to use JDBC to achieve database connection, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

The abbreviation of JDBC:Java database connection technology (Java DataBase Connectivity) provides the ability to connect to a variety of commonly used databases. JDBC client-database server (JSP/Servlet) JDBC client-application server-database server 1. Programmers use the interface, class DriverManager: load a variety of different JDBC drivers (connect different databases) Connection interface: responsible for connecting the database and acting as data transfer Statement interface: generated by Connection, responsible for executing SQL statements PreparedStatement interface: responsible for executing SQL statements, can contain placeholders in the statement ResultSet interface

Responsible for saving the query result set after the execution of Statement 2.

JDBC application: 1. Load the JDBC driver 2. Create a connection to the database 3. Create statement object-Statement / PreparedStatement create statement object: Statement or PreparedStatement such as: Statement stmt = con.createStatement (); PreparedStatement pstmt = con.prepareStatement (sql); 4. Execute the SQL statement using the statement object to get the result executeQuery ()-select executeUpdate ()-insert,delete,update stmt.executeUpdate (sql); or pstmt.executeUpdate (); 5. Processing result

Return the ResultSet object and use the get × × ("parameter") method to get different types of result values: getInt (), getString (), getFloat (), getDate ()...

If (rs.next ()) {f.setCreator (rs.getString ("creator")); f.setTitleName (rs.getString ("titleName")); f.setCreateTime (rs.getDate ("createTime")); f.setId (id);}

6. Close

Try {if (rsshipping null) resultset.close (); if (pmtshipping null) pmt.close (); if (cased null) connection.close () } catch (SQLException e) {e.printStackTrace ();}

3. Connection to database (1) pure java-driven connection

/ / sqlserver database connection String URL= "jdbc:sqlserver://localhost:1433; DatabaseName=news"; Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection (URL, "sa", "sa"); / / mysql database connection String URL= "jdbc:mysql://localhost:3306/news" Class.forName ("com.mysql.jdbc.Driver"); / / mysql Connection con = DriverManager.getConnection (URL, "root", "123456")

Common database connections 1, MySQL

URL:jdbc:mysql://localhost:3306/ database name driver class for connecting MySQL: com.mysql.jdbc.Driver 2, SQLServer2000 connection SQLServer2000 URL:jdbc: microsoft:sqlserver://localhost:1433;DatabaseName= database name driver class: com.microsoft.jdbc.sqlserver.SQLServerDriver 3, SQLServer2005 connection SQLServer2005 URL:jdbc: sqlserver://localhost:1433;DatabaseName= database name driver class: com.microsoft. Sqlserver .jdbc.SQLServerDriver 4, URL:jdbc:oracle:thin:@localhost:1521 of Oracle connection Oracle: database name driver class: oracle.jdbc.driver.OracleDriver (2) JDBC-ODBC for bridging

/ / driver name, be careful not to miswrite a letter Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver")

/ / Database name news Connection con = DriverManager.getConnection ("jdbc:odbc:news", "sa", "sa")

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report