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

Sample code and precautions for using JDBC to connect to a database in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "sample code and precautions of using JDBC to connect to database in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the sample code and precautions of using JDBC to connect to the database in Java.

The example code is as follows:

Import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.sql.PooledConnection; import oracle.jdbc.pool.OracleConnectionPoolDataSource; public class JDBCTest {private String url = null / * * / public JDBCTest (String sHostName, String sPortNumber, String sSid) {url = "jdbc:oracle:thin:@" + sHostName + ":" + sPortNumber + ":" + sSid; / / if JDK1.6 you also can use as / / url = "jdbc:oracle:thin:@" + sHostName + ":" + sPortNumber + "/" + sSid } public List getList (String sUsrName, String sPassword, String sql) {List resultList = new ArrayList (); try {OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource (); String url1 = System.getProperty ("JDBC_URL"); if (url1! = null) url = url1; ocpds.setURL (url) Ocpds.setUser (sUsrName); ocpds.setPassword (sPassword); PooledConnection pc = ocpds.getPooledConnection (); Connection conn = pc.getConnection (); PreparedStatement pstmt = conn.prepareStatement (sql); ResultSet rset = pstmt.executeQuery (); while (rset.next ()) {resultList.add (rset.getString (1)) } rset.close (); pstmt.close (); conn.close (); pc.close ();} catch (Exception e) {} return resultList } / * * @ param args * / public static void main (String [] args) {/ / use you real info String sUsrName = ""; String sPassword = ""; String sql = ""; JDBCTest jdbctest = new JDBCTest ("localhost", "1521", "orcl"); List list = jdbctest.getList (sUsrName, sPassword, sql) System.out.println (list.size ());}}

Special attention should be paid to:

Url = "jdbc:oracle:thin:@" + sHostName + ":" + sPortNumber + "/" + sSid

In JDK1.6, you can use url. That is, port and sid can be separated by "/". You can't use this in JDK 1.5, you can only use ":".

In addition to using the above connection pool to connect, of course, you can also use the traditional way to connect.

Class.forName ("oracle.jdbc.driver.OracleDriver"). NewInstance (); Connection conn = DriverManager.getConnection (url, suUserName,passWord); at this point, I believe you have a deeper understanding of "sample code and precautions for connecting to a database using JDBC in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report