您的当前位置:首页正文

数据库连接及增删改查

2020-11-27 来源:我们爱旅游
连接数据库

首先把需要的jar包拷到lib文件夹下,其中包括mysql的驱动

一、新建项目 spring(Dynamic Web Project)

二、src下创建package(cn.com.utils),spring-context.xml(new-other-xml-xml file)

spring-context.xml代码如下:

xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:mvc=\"http://www.springframework.org/schema/mvc\"

xmlns:tx=\"http://www.springframework.org/schema/tx\" xmlns:context=\"http://www.springframework.org/schema/context\"

xsi:schemaLocation=\"http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

name:

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd\">

class=\"org.springframework.jdbc.datasource.DriverManagerDataSource\">

(路)

class=\"org.springframework.jdbc.core.JdbcTemplate\" >

id=\"jdbcTemplate\"

(通往路的交通工具)

三、cn.com.utils下创建Department.java、SpringUtils.java(class类)

Department.java代码如下:

package cn.com.utils;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

public class Department {

private JdbcTemplate jdbcT = (JdbcTemplate) SpringUtils

.getBean(\"jdbcTemplate\");

public void findALLBooks() {

String sql = \"select * from user\";

List list = jdbcT.queryForList(sql);

Iterator iterator = list.iterator();

while (iterator.hasNext()) {

Map map4book = (Map) iterator.next();

System.out.println((Integer)map4book.get(\"id\"));

System.out.println((String)map4book.get(\"name\"));

}

}

public int delete(int bid){

String sql = \"delete from BookInfo where bid =?\";

return jdbcT.update(sql, new Object[]{bid});

}

public static void main(String[] args) {

Department d = new Department();

d.findALLBooks();

}

}

SpringUtils.java代码如下:

package cn.com.utils;

import org.springframework.context.ApplicationContext;

import

org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringUtils {

private static ApplicationContext ctx = new

ClassPathXmlApplicationContext(\"spring-context.xml\");

public static Object getBean(String beanName){

return ctx.getBean(beanName);

}

}

Department.java代码(增删改查)

package cn.com.utils;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

public class Department {

private JdbcTemplate jdbcT = (JdbcTemplate) SpringUtils

.getBean(\"jdbcTemplate\");

//增加

public void addBooks(){

String sql = \"insert into user (id, name) values ('4', 'zhaoliu')\";

int result = jdbcT.update(sql);

if(result > 0){

System.out.println(\"操作成功\");

}else{

System.out.println(\"操作失败\");

}

}

//修改

public void updateBooks(){

String sql =\"update user set name='sunwukong111' where id=4\";

int result = jdbcT.update(sql);

if(result > 0){

System.out.println(\"操作成功\");

}else{

System.out.println(\"操作失败\");

}

}

//删除delete Form user where id = 4

public void deleteBooks(){

String sql =\" delete from user where id =3 \";

int result = jdbcT.update(sql);

if(result > 0){

System.out.println(\"操作成功\");

}else{

System.out.println(\"操作失败\");

}

}

public void findALLBooks() {

String sql = \"select * from user\";

List list = jdbcT.queryForList(sql);

Iterator iterator = list.iterator();

while (iterator.hasNext()) {

Map map4book = (Map) iterator.next();

System.out.println((Integer)map4book.get(\"id\"));

System.out.println((String)map4book.get(\"name\"));

}

}

public int delete(int bid){

String sql = \"delete from BookInfo where bid =?\";

return jdbcT.update(sql, new Object[]{bid});

}

public static void main(String[] args) {

Department d = new Department();

//d.findALLBooks();

//d.addBooks();

//d.updateBooks();

d.deleteBooks();

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容