हम्म ... किसी भी तरह, DataSourceUtils
की जावाडोक अधिक "सही" है (मेरा मतलब है, डॉक नहीं गलत है लेकिन तकनीकी रूप से, आप एक डेटास्रोत से कनेक्शन प्राप्त - JNDI के माध्यम से प्राप्त किया जा सकता है कि):
सहायक वर्ग जो DataSource
से जेडीबीसी कनेक्शन प्राप्त करने के लिए स्थिर तरीके प्रदान करता है।
और निम्न विधियों में होना चाहिए कि आप क्या देख रहे हैं:
बेसिक उदाहरण (MySQL documentation से):
// Create a new application context. this processes the Spring config
ApplicationContext ctx = new ClassPathXmlApplicationContext("ex1appContext.xml");
// Retrieve the data source from the application context
DataSource ds = (DataSource) ctx.getBean("dataSource");
// Open a database connection using Spring's DataSourceUtils
Connection c = DataSourceUtils.getConnection(ds);
try {
// retrieve a list of three random cities
PreparedStatement ps = c.prepareStatement(
"select City.Name as 'City', Country.Name as 'Country' " +
"from City inner join Country on City.CountryCode = Country.Code " +
"order by rand() limit 3");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
String city = rs.getString("City");
String country = rs.getString("Country");
System.out.printf("The city %s is in %s%n", city, country);
}
} catch (SQLException ex) {
// something has failed and we print a stack trace to analyse the error
ex.printStackTrace();
// ignore failure closing connection
try { c.close(); } catch (SQLException e) { }
} finally {
// properly release our connection
DataSourceUtils.releaseConnection(c, ds);
}
स्रोत
2010-09-08 23:59:42
हाय, मैं वसंत के लिए नया हूँ, और मैं अगर मैं विधि आप ऊपर बताए उपयोग कर रहा हूँ जानना चाहते हैं, जहां मैं 'username' और' के लिए password' स्टोर करते हैं मेरे कनेक्शन? –