android studio怎么用jdbc的方式连接数据库
1个回答
展开全部
实例代码如下: 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<String> getList(String sUsrName, String sPassword, String sql) { List<String> resultList = new ArrayList<String>(); 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<String> list = jdbctest.getList(sUsrName, sPassword, sql); System.out.println(list.size()); } }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询