In spring, a sample of the MySQL database was added and hacked, very easy.
File structure
MAVEN's Pom.xml file, which contains several very important jar packages
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>MySpring</groupId> <artifactid>myspring </artifactId> <version>0.0.1-SNAPSHOT</version> <name>MySpring</name> < description>myspring</description> <dependencies> <dependency> <groupid>org.spring Framework</groupid> <artifactId>spring-context</artifactId> <VERSION>4.1.0.RELEASE&L T;/version> </dependency> <dependency><groupId>org.springframework</groupId>< Artifactid>spring-jdbc</artifactid><version>4.1.0.release</version></dependency> <dependency> <groupId> mysql</groupid> <artifactId> MYSQL-CONNECTOR-JAVA≪/artifactid> <version> 5.0.5</version> </dependency></dependencies> </PROJECT&G T
Operation class
Package Nx.database;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.SQLException; Import Javax.sql.datasource;public class Studentsdaoimpl implements Studentsdao{private DataSource Datasource;public DataSource Getdatasource () {return DataSource;} public void Setdatasource (DataSource DataSource) {this.datasource = DataSource;} public void Savestudents () {Connection conn=null; PreparedStatement stmt=null;try {conn=datasource.getconnection (); Stmt=conn.preparestatement ("INSERT into ' data '. Student ' (' id ', ' name ') VALUES (NULL, ' Jiang '); "); Stmt.execute (); Stmt.close (); Conn.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println ("Save student Information successfully. ");}}
Beans.xml (Note that the password of the database is replaced by your own password)
<?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=" DataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource "><property name=" Driverclassname "value=" Com.mysql.jdbc.Driver "></property> <property name=" url "value=" jdbc:mysql://www.itbuluoge.com : 3306/wordpress "></property> <property name=" username "value=" root "></property> <property name= "Password" value= "1234451" ></property> </bean><bean id= "Userimpl" class= " Nx.database.StudentsDaoImpl "><property name=" DataSource "ref=" DataSource "></property> </ Bean></beans>
Test class
Package Nx.database;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {/** * @param args */public static void Main (string[] args) {//TODO auto-generated method Stubapplicationcontext ctx=new Classpathxmlapplicationcontext ("Nx/database/beans.xml"); Studentsdaoimpl userimpl= (Studentsdaoimpl) Ctx.getbean ("Userimpl"); Userimpl.savestudents ();}}
After you have finished, look at the database and find that you have the data
Working with MySQL database in spring