In spring, the MySQL database is an example of an additional pruning and checking, which is very simple.
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 success!") ");}}
Beans.xml (Note that the password for the database is replaced with your own password)
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x Si= "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.spring Framework.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 ();}}
When you're done, look at the database and see that you've got the data
Working with MySQL database in spring