Working with MySQL database in spring

Source: Internet
Author: User

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> &LT;VERSION&GT;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&Lt;/artifactid> <version> 5.0.5</version> </dependency></dependencies> &LT;/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&gt ;</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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.