Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 11:16:47 10月04日 Update

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!

Tags: Code events data databases notes examples content methods learning practical deeper between that is traditional interests such as above examples practical practical easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn NVidia OPPO Reno Xiaomi