本文实例为大家分享了spring通过jdbc连接数据库的具体代码,供大家参考,具体内容如下
首先看下整个工程的架构目录:
需要的jar包:
一、建表
create table student( id int primary key auto_increment, name varchar(32), age int, phone varchar(32));二、新建与数据库对应JavaBean
package com.etoak.bean; public class Student { /** * 一个标准的javaBean对象 : * 表字段对应的属性 * 属性对应的getter、setter方法 * 无参构造器 * 除id[主键]之外其他参数组成的构造器 * 所有参数组成的构造器 */ private Integer id; private String name; private Integer age; private String phone; public Student() { super(); } public Student(String name, Integer age, String phone) { super(); this.name = name; this.age = age; this.phone = phone; } public Student(Integer id, String name, Integer age, String phone) { super(); this.id = id; this.name = name; this.age = age; this.phone = phone; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }}三、spring的applicationContext配置文件
<beans xmlns="http://.etoak.dao.StuDaoImpl; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); StuDaoImpl dao = (StuDaoImpl)ac.getBean("dao"); Student stu = new Student("sheldon",30,"111"); boolean flag = dao.addStu(stu); System.out.println(flag); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。