First of all, the database is like this, very simple.
Of course, to introduce spring's package, I am all imported here, easy.
The applicationcontext.xml is like this:
<?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-2.5.xsd ">
<bean id= "Test" class= jdbc. Test ">
<property name=" DataSource "ref=" DataSource "></property>
</bean>
< Bean id= "DataSource"
class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name=
"url" value= "Jdbc:mysql ://localhost:3306/testspring "/> <property name=" username "value=" "root"/> <property name=
" Password "value=" "/>
</bean>
</beans>
The User.java is like this:
Package jdbc;
public class User {
private int id;
private String name;
private String password;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String GetPassword () {return
password;
}
public void SetPassword (String password) {
this.password = password;
}
}
Here are three ways to compare:
1, Spring
Package JDBC;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import java.sql.SQLException;
Import Javax.sql.DataSource;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {private DataSource DataSource;
public void Setdatasource (DataSource DataSource) {this.datasource = DataSource; public void Insert (User u) {String sql = ' INSERT into _user ' + ' VALUES (null,?,?) ';
/Normal SQL statement Connection conn = null;
try {conn = datasource.getconnection ();
PreparedStatement PS = conn.preparestatement (SQL);
Ps.setstring (1, U.getname ());
Ps.setstring (2, U.getpassword ());
Ps.executeupdate ();
Ps.close ();
catch (SQLException e) {throw new RuntimeException (e);
finally {if (conn!= null) {try {conn.close (); ' Catch (SQLException e) {}}} ' public static void main (string[] args) {ApplicationContext ctx = new ClAsspathxmlapplicationcontext ("Applicationcontext.xml");
Test T = (test) ctx.getbean ("test");
User U = new user ();
U.setname ("DD");
U.setpassword ("DD");
T.insert (U);
}
}
The result of the run is this:
2, JdbcTemplate
package jdbc;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import java.sql.SQLException;
Import Javax.sql.DataSource;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.jdbc.core.JdbcTemplate;
public class Test {private DataSource DataSource;
public void Setdatasource (DataSource DataSource) {this.datasource = DataSource; public void Insert (User u) {String sql = ' INSERT into _user ' + ' VALUES (null,?,?) ';
/Normal SQL statement JdbcTemplate template = new JdbcTemplate (DataSource);
Template.update (SQL, New Object[]{u.getname (), U.getpassword ()}); public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Applicati
Oncontext.xml ");
Test T = (test) ctx.getbean ("test");
User U = new user ();
U.setname ("DD");
U.setpassword ("DD");
T.insert (U); }
}
Can see a lot simpler, do not connection.
3, Jdbcdaosupport
This is simpler than new JdbcTemplate.
Package jdbc;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.jdbc.core.support.JdbcDaoSupport;
The public class Test extends Jdbcdaosupport {
//jdbcdaosupport class already has the public final void Setdatasource (DataSource DataSource)
///No Override and cannot override public
void Insert (User u) {
String sql = ' INSERT into _user ' + ' VALUES (null,?,?) "//Normal SQL statement
this.getjdbctemplate (). Update (SQL, new Object[]{u.getname (), U.getpassword ()});
public static void Main (string[] args) {
ApplicationContext ctx = new Classpathxmlapplicationcontext (
" Applicationcontext.xml ");
Test T = (test) ctx.getbean ("test");
User U = new user ();
U.setname ("DD");
U.setpassword ("DD");
T.insert (u);
}
}
Three methods which are simpler to glance at.
I refer to the article: Spring + jdbctemplate + jdbcdaosupport examples