JdbcTemplate 对mysql数据库操作

来源:互联网 发布:horner算法 编辑:程序博客网 时间:2024/05/22 06:12
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() {@Overridepublic 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;   }}
原创粉丝点击