1.3.?Spring连接UXDB

1.3.1. 安装SpringIDE
1.3.2. 导入spring所有驱动包
1.3.3. 导入commons驱动包
1.3.4. 创建Bean类、接口类、接口实现类、beans.xml配置和测试类
1.3.5. 运行结果
1.3.6. 使用分析说明

1.3.1.?安装SpringIDE

下载离线包或者在线安装SpringIDE插件。

1.3.2.?导入spring所有驱动包

导入官网下载的spring驱动包。

1.3.3.?导入commons驱动包

导入官网下载的commons驱动包。

1.3.4.?创建Bean类、接口类、接口实现类、beans.xml配置和测试类

右键uxdb-jdbc,New->Source Folder,Folder name输入spring。

创建包(右键spring,New->Package)com.uxdb.bean,com.uxdb.service,com.uxdb.service.impl,com.uxdb.springtest。

包下面创建对应的类(右键包名,New->Class) StudentBean,StudentService,StudentServiceImpl,SpringJdbcTest。

创建Spring Bean配置文件(右键spring,New->Other...->Spring->Spring Bean Configuration File,点击Next,File name中输入Application-context,点击Finish)。

  1. StudentBean.java;

    package com.uxdb.bean;
    public class StudentBean {
    	private int sno;	
    	private String sname;	
    	private int sage;	
    	private String ssex;	
    	public int getSno() {
    		return sno;
    	}
    	public void setSno(int sno) {
    		this.sno = sno;
    	}
    	public String getSname() {
    		return sname;
    	}
    	public void setSname(String sname) {
    		this.sname = sname;
    	}
    	public int getSage() {
    		return sage;
    	}
    	public void setSage(int sage) {
    		this.sage = sage;
    	}
    	public String getSsex() {
    		return ssex;
    	}
    	public void setSsex(String ssex) {
    		this.ssex = ssex;
    	}
    }

  2. StudentService.java;

    package com.uxdb.service;
    import java.util.List;
    import com.uxdb.bean.StudentBean;  
    public interface StudentService {  
        //保存  
        public void save(StudentBean student);  
        //更新  
        public void update(StudentBean student);  
        //获取  
        public StudentBean getStudent(int sno);  
        public List<StudentBean> getStudentBean();  
        //删除记录  
        public void delete(int sno);
    } 

  3. StudentServiceImpl.java;

    package com.uxdb.service.impl;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    import com.uxdb.bean.StudentBean;
    import com.uxdb.service.StudentService;
    public class StudentServiceImpl implements StudentService {
    	private JdbcTemplate jdbcTemplate;
    	public void setDataSource(DataSource dataSource) {
    		this.jdbcTemplate = new JdbcTemplate(dataSource);
    	}
    	@Override
    	public void save(StudentBean student) {
    		// TODO Auto-generated method stub
    		jdbcTemplate.update("insert into student(sno,sname,sage,ssex) values(?,?,?,?)",
    				new Object[] { student.getSno(), student.getSname(), student.getSage(), student.getSsex() },
    				new int[] { java.sql.Types.INTEGER,java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.VARCHAR });
    	}
    	@Override
    	public void update(StudentBean student) {
    		// TODO Auto-generated method stub
    		jdbcTemplate.update("update student set sname=?,ssex=? where sno=?", 
    				new Object[] { student.getSname(), student.getSsex(), student.getSno() },
    				new int[] { java.sql.Types.VARCHAR,java.sql.Types.VARCHAR,java.sql.Types.INTEGER });
    	}
    	@SuppressWarnings("unchecked")
    	@Override
    	public StudentBean getStudent(int sno) {
    		// TODO Auto-generated method stub
    		return (StudentBean) jdbcTemplate.queryForObject("select * from student where sno=?", 
    				new Object[] { sno },
    				new int[]{java.sql.Types.INTEGER},new StudentRowMapper() );
    	}
    	@SuppressWarnings("unchecked")
    	@Override
    	public List<StudentBean> getStudentBean() {
    		// TODO Auto-generated method stub
    		return (List<StudentBean>)jdbcTemplate.query("select * from student",   
                    new StudentRowMapper() ); 
    	}	
    	@SuppressWarnings("rawtypes")
    	public class StudentRowMapper implements RowMapper {    
    	    @Override  
    	    public Object mapRow(ResultSet rs, int n) throws SQLException {  
    	        // TODO Auto-generated method stub  
    	    	StudentBean student=new StudentBean();
    			student.setSno(rs.getInt("sno"));
    			student.setSname(rs.getString("sname"));
    			student.setSage(rs.getInt("sage"));
    			student.setSsex(rs.getString("ssex"));
    			return student;  
    	    }  	  
    	} 	
    	@Override
    	public void delete(int sno) {
    		// TODO Auto-generated method stub
    		jdbcTemplate.update("delete from student where sno=?", new Object[] { sno },
    				new int[] { java.sql.Types.INTEGER });
    	}
    }

  4. Application-context.xml;

    <?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.xsd"> 
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
              <property name="driverClassName" value="com.uxsino.uxdb.Driver"/>  
              <property name="url" value="jdbc:uxdb://192.168.1.82:5432/test"/>  
              <property name="username" value="uxdb"/>  
              <property  name="password" value="123456"/>  
        </bean>  
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
              <property name="dataSource" ref="dataSource"/>  
        </bean>  
        <bean id="studentService" class="com.uxdb.service.impl.StudentServiceImpl">  
              <property name="dataSource" ref="dataSource"></property>  
        </bean>  
    </beans>

  5. SpringJdbcTest.java;

    package com.uxdb.springtest;
    import java.util.List;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.uxdb.bean.StudentBean;
    import com.uxdb.service.StudentService;
    public class SpringJdbcTest {
    	private static  StudentService studentService;
    	private static ApplicationContext act; 
    	public static void main(String[] args) {
    		act = new ClassPathXmlApplicationContext("Application-context.xml");  
    		studentService=(StudentService) act.getBean("studentService");  
    		List<StudentBean> list;
    		StudentBean student = new StudentBean();		
    		student.setSno(8008);
    		student.setSname("springname");
    		student.setSage(66);
    		student.setSsex("女");
    		studentService.save(student);		
    		list = studentService.getStudentBean();
    		System.out.println("newstudent");
    		for (StudentBean u : list) {
    			System.out.println(u.getSno() + ";" + u.getSname() + ";" + u.getSage() + ";" + u.getSsex());
    		}		
    		student.setSno(1005);
    		student.setSname("tnm");
    		student.setSsex("男");
    		studentService.update(student);		
    		student = studentService.getStudent(1005);
    		System.out.println("");
    		System.out.println("updatestudent");
    		System.out.println(student.getSno() + ";" + student.getSname() + ";" + student.getSage() + ";" + student.getSsex());		
    		//studentService.delete(8008);		
    		list = studentService.getStudentBean();
    		System.out.println("");
    		System.out.println("student");
    		for (StudentBean u : list) {
    			System.out.println(u.getSno() + ";" + u.getSname() + ";" + u.getSage() + ";" + u.getSsex());
    		}
    	}
    }

1.3.5.?运行结果

运行结果如下显示时spring连接使用uxdb成功,显示student表的数据与uxdb数据库中查看结果一致。

1.3.6.?使用分析说明

配置Application-context.xml文件注意以下几点:

  1. driverClassName="com.uxsino.uxdb.Driver" (使用uxdb的driver.class)

  2. url="jdbc:uxdb://192.168.1.223:5432/test" (jdbc:uxdb://IP:port/databasename)

  3. username="uxdb"

  4. password="123456"

XML 地图 | Sitemap 地图