• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDBC connection

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used spring DataSource to get the connection string and use it to run the queries can any one suggest was this the right way

Created a class database

public class database {


public static Connection connect()
{

Connection conn=null;


try
{

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml",database.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource", DataSource.class);
conn=dataSource.getConnection();

}
catch(SQLException e)
{
e.getMessage();

}
return conn;
}

}

Bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/finance"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>

<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>

<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

I started Using the connection String the way give below in the class.


public class accountdao {

ResultSet rs = null;
Connection conn = null;
PreparedStatement ps = null;

public int insertaccount(String customerid, String typeofaccount, String principalamount, String rateofinterest, String accountstartdate, String status, String interestperday, String interestpermonth, String gurantorid) {


System.out.println("gurantoridgurantoridgurantorid" + gurantorid);

String dateofinterest = "";
ResultSet rs = null;
Connection conn = null;
Statement ps = null;
String sql1 = "";
int i = 0;
int k = 0;
//String Account="saving";


try {
conn = database.connect();
ps = conn.createStatement();




String sql = "INSERT INTO customer_account(Account_Type,PrincipalAmount,RateofInterest,AccountStartDate,InterestDueDate,Status,Interestperday,Interestpermonth,finance_customer_Customer_ID) VALUES("
+ "'" + typeofaccount + "','" + principalamount + "','" + rateofinterest + "','" + accountstartdate + "', "
+ "'" + dateofinterest + "','" + status + "','" + interestperday + "'," + interestpermonth + " ," + customerid + ")";


i = ps.executeUpdate(sql);


} catch (Exception e) {

e.printStackTrace();
} finally {
try {
/**
* Close the preparedStatement
*/
if (ps != null) {
ps.close();
}
/**
* Close the connection
*/
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
/**
* Handle any exception
*/
e.printStackTrace();
}
}




return i;

}

I called the connect method of the database class.


please if i have wrongly used the Spring frame work data source to get the connection string.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the CODE tags when posting code or configuration so that we can read it. Thanks, CODE tags keeps the indentation of your code, without indentation it is unreadable.

Thanks

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you need to even create that Database class?

You shouldn't have to deal with any boilerplate, low level, resource management code. The JdbcTemplate does all that already for you. To run a query you just call a method on jdbcTemplate. So your code to run a query is either 1 or 2 lines of code.

In your config file you have a bean for JdbcTemplate. Just inject that into your DAO/Repositories and call methods on it in your Repo methods.

Mark
 
ajay kumar singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Thanks you are right but i have already used the above way in the project,cant replace it with jdbc template can i go with that ....
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ajay kumar singh wrote:Hi Mark,

Thanks you are right but i have already used the above way in the project,cant replace it with jdbc template can i go with that ....



Why can't you change it? You should change it. It is called refactoring and it is a tool that every developer should be using daily. Not months down the road when you have tightly couple, low cohesion, spaghetti, unmaintainable code.

I wouldn't even be able to answer your question knowing that is the wrong way to go. It isn't in my DNA to show or recommend wrong solutions.

I hope you understand.

Mark
 
ajay kumar singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Thanks, for the technical suggestion i got the point will implement the same.
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic