dbUrl=jdbc:mysql://localhost:3306/db_spiter_meituan?serverTimezone=GMT%2B8 #这种是错误换成上面的就正常了 # dbUrl=jdbc:mysql://localhost:3306/db_spiter_meituan?useUnicode=true&characterEncoding=UTF-8 #这种是错误换成上面的就正常了 dbUserName=root dbPassword=123456 jdbcName=com.mysql.cj.jdbc.Driver
这是在spirng boot项目中,因为我在写爬虫selenium,所以使用jdbc连接数据。 添加数据使用。做一些简单的操作。
报错如下
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
package com.spider.selenium; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * proerties文件工具类 */ public class PropertiesUtil { /** * 根据key获取value * @param key 属性值 * @return */ public static String getValue(String key){ Properties prop=new Properties(); InputStream in=new PropertiesUtil().getClass().getResourceAsStream("/mysql.properties"); try { prop.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return (String)prop.get(key); } }
package com.spider.selenium; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库工具类 * @author * */ public class DbUtil { /** * 获取数据库连接 * @return * @throws Exception */ public Connection getCon() throws Exception{ Class.forName(PropertiesUtil.getValue("jdbcName")); Connection con=DriverManager.getConnection(PropertiesUtil.getValue("dbUrl"), PropertiesUtil.getValue("dbUserName"), PropertiesUtil.getValue("dbPassword")); return con; } /** * 关闭数据库连接 * @param con * @throws Exception */ public void closeCon(Connection con)throws Exception{ if(con!=null){ con.close(); } } public static void main(String[] args) { DbUtil dbUtil=new DbUtil(); try { dbUtil.getCon(); System.out.println("数据库连接成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("数据库连接失败"); } } }
package com.spider.selenium; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Main { private static DbUtil dbUtil = new DbUtil(); public static void main(String[] args) throws Exception { Insert(); String sql = "SELECT * FROM t_a_role "; Connection con = null; PreparedStatement pstmt = null; try{ con=dbUtil.getCon(); pstmt=con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ String name = rs.getString("name"); System.out.println(name); } }catch(Exception e){ e.printStackTrace(); }finally { try { pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } /* public static void updateData(Article article){ Connection con=null; PreparedStatement pstmt=null; try{ con=dbUtil.getCon(); String sql="update t_article set name=?,content=?,include_date=now(),share_user=?,share_date=?,state=? where id=?"; pstmt=con.prepareStatement(sql); pstmt.setString(1,article.getName()); pstmt.setString(2,article.getContent()); pstmt.setString(3,article.getShare_user()); pstmt.setString(4,article.getShare_date()); pstmt.setInt(5,article.getState()); pstmt.setInt(6,article.getId()); pstmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); }finally { try { pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } */ public static void Insert() throws Exception { Connection con=null; con=dbUtil.getCon(); //调用 DBconnection 类的 conn() 方法连接数据库 PreparedStatement pstmt=null; String sql = "INSERT INTO t_a_role (create_date_time,name,order_no,remark,update_date_time) VALUES(?,?,?,?,?)"; //插入sql语句 try { pstmt = con.prepareStatement(sql); pstmt.setString(1, "2012-12-12 22:00"); pstmt.setString(2, "1"); pstmt.setInt(3, 2); pstmt.setString(4, ""); pstmt.setString(5, null); pstmt.executeUpdate(); //执行sql语句 System.out.println("插入成功(* ̄︶ ̄)"); } catch (SQLException e) { e.printStackTrace(); }finally { dbUtil.closeCon(con); } } }
package com.spider.selenium; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.transaction.Synchronization; import com.spider.config.DataIndex; import com.spider.entity.Data; import com.spider.util.DateUtil; public class DataUtil { private static DbUtil dbUtil = new DbUtil(); private static DataIndex dataIndex = new DataIndex() ; public static synchronized void Insert(Data data) throws Exception { Connection con=null; con=dbUtil.getCon(); //调用 DBconnection 类的 conn() 方法连接数据库 PreparedStatement pstmt=null; String sql = "INSERT INTO t_data (address,create_date_time,name,pingfen,renjun,ping,diqu) VALUES(?,?,?,?,?,?,?)"; //插入sql语句 try { pstmt = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); pstmt.setString(1, data.getAddress()); pstmt.setString(2, DateUtil.formatDate(data.getCreateDateTime(), "yyyy-MM-dd HH:mm") ); pstmt.setString(3, data.getName()); pstmt.setString(4, data.getPingfen()); pstmt.setString(5, data.getRenjun()); pstmt.setString(6, data.getPing()); pstmt.setString(7, data.getDiqu()); pstmt.executeUpdate(); //执行sql语句 ResultSet rs=pstmt.getGeneratedKeys(); while (rs.next()) { int id=rs.getInt(1);//返回指定列的值 System.out.println("自增id="+id); data.setId(id); } System.out.println("插入成功(* ̄︶ ̄)"); //添加索引 dataIndex.addIndex(data); //添加索引 } catch (SQLException e) { e.printStackTrace(); }finally { dbUtil.closeCon(con); } } }
站长微信:xiaomao0055
站长QQ:14496453