Json_Nie的专栏

一个自卑但又坚信勤能补拙的菜鸟

JdbcTemplate 对mysql数据库操作

package Dao;

import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.DriverManager;  
import java.sql.Connection;  
import java.sql.Statement;  

import org.apache.coyote.http11.filters.VoidInputFilter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import bsh.This;

//import Domain.model;

public class Mysqltest {

	
	   private static JdbcTemplate temp = new JdbcTemplate();
	   private static int temp2;
	   public static String URL;
       public static String uesrName;
	   public static String passWord;
	   public static int ID = 1;

	   public static void main(String[] args) throws Exception {
		   
		   connectToDatabase();
		   //creattable();
		   //useradd();
		   User temp = Search();
		   System.out.println(temp.getName());
		   System.out.println(temp.getCompany());
		   System.out.println(temp.getEmailUrl());
		   System.out.println(temp.getId());
	   }
	   
	   //connect to the mysql
	   public static void connectToDatabase() {
		   
			DriverManagerDataSource ds = new DriverManagerDataSource();
			ds.setDriverClassName("com.mysql.jdbc.Driver");
			
			ds.setUrl("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8");		
			ds.setUsername("root");
			ds.setPassword("2323");
			temp.setDataSource(ds);
		}	
	  //creat the table
	   public static void creattable() {
		   String sql="CREATE TABLE user_in (ID INT(60) primary key, EmailUrl VARCHAR(100),"
				   + " name VARCHAR(60), password VARCHAR(100), Company VARCHAR(20));";		
		   temp.execute(sql);
	   }
	   
	   //insert the information
	   public static void useradd() {
		   String sqlStr = "INSERT INTO " + "user_in" + "(ID,EmailUrl,name,password,Company) " + " VALUES(?,?,?,?,?) ";
			Object args[] = new Object[] {"1", "weizhinie@tju.edu.cn","weizhi","2323","天津大学"};

			temp.update(sqlStr, args);
	   }
	   
	   
	   public static void useradd2(String name, String password, String email, String company) {
		   String sqlStr = "INSERT INTO " + "user_in" + "(ID,EmailUrl,name,password,Company) " + " VALUES(?,?,?,?,?) ";
		   ID++;
		   Object args[] = new Object[] {String.valueOf(ID), email,name,password,company};

			temp.update(sqlStr, args);
	   }
	   
	   //search the information
	   public static User Search() {
		   User model = new User();
		   ArrayList<User> result = new ArrayList<>();			
			String sqlStr = String.format("SELECT * FROM %s where name like ? ", "user_in");						   
			temp.query(sqlStr, new Object[]{"%weizhi%"}, new RowCallbackHandler() {			
				@Override
				public void processRow(ResultSet arg0) throws SQLException {
					
					System.out.println(arg0);
					model.setCompany(arg0.getString("Company"));
					model.setPassword(arg0.getString("password"));
					//model.setImgWebURL(arg0.getString("imgWebURL"));	
					model.setName(arg0.getString("name"));
					model.setEmailUrl(arg0.getString("EmailUrl"));
					
										
					result.add(model);
				}
			});
 
			return model;
	   }
	   //search the number of the result. 
	   public static int Search2(String name, String password) throws Exception {
		   //User model = new User();
		    
		    temp2 = 1;
		   //ArrayList<User> result = new ArrayList<>();	
		    String query = "SELECT * FROM user_in where name like \"%"+name+"%\"";
		    System.out.println(query);
		    List rows = temp.queryForList(query);
			
					
					if(rows.size()==0) {
						System.out.println("meiyou");
						temp2 = 0;
					}
					else {
					//System.out.println(arg0);
						System.out.println("you");
						temp2 = 1;						
					}
            return temp2;
	   }
}
阅读更多
个人分类: Java Web服务器
想对作者说点什么? 我来说一句

SpringMVC整合jdbcTemplate实例

2015年05月14日 11.84MB 下载

springAop多数据源

2015年12月03日 19KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭