In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.