?Sat Oct 01 13:27:30 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update) values('GTX TITAN 1060','顯卡','NVIDIA',8999.0,'2016-10-01',current_da' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192) at dao.inaDao.addIna(inaDao.java:31) at action.InaAction.main(InaAction.java:30)這個是異常,刪除方法可用,但增刪改用不了package action;/**?* Action類,調(diào)用Dao層方法,進(jìn)行具體的數(shù)據(jù)操作,視圖層?*/import java.util.Date;import model.Ina;import dao.inaDao;public class InaAction { public static void main(String[] args) throws Exception { inaDao g=new inaDao(); //List<Ina> gsList=gDao.query(); Ina gIna=new Ina(); gIna.setName("GTX TITAN 1060"); gIna.setCate("顯卡"); gIna.setBrandname("NVIDIA"); gIna.setPrice((float) 8999.000); gIna.setDate(new Date());//這個date是util類型的 gIna.setCreate_date(new Date()); gIna.setUpdate(new Date()); //gIna.setId(8); //g.updateIna(gIna); //g.delIna(gIna); g.addIna(gIna);// Ina gIna2=g.get(6);// System.out.println(gIna2.toString()); }}package dao;/**?* Dao層封裝增刪改查方法,業(yè)務(wù)邏輯層?*/import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import db.DBUtil;import model.Ina;public class inaDao { public void addIna(Ina a) throws Exception { Connection connection=DBUtil.getConnection(); String sql=" "+ " insert into tdb_goods"+ " (name,cate,brandname,price,date,create_date,update)"+ " values(?,?,?,?,?,current_date(),current_date())"; PreparedStatement ptmt=connection.prepareStatement(sql); ptmt.setString(1, a.getName()); ptmt.setString(2, a.getCate()); ptmt.setString(3, a.getBrandname()); ptmt.setFloat(4, a.getPrice()); ptmt.setDate(5, new Date(a.getDate().getTime()));//傳進(jìn)來的是java.util類型的date,但需要的是java.sql類型,所以要進(jìn)行類型轉(zhuǎn)換 ptmt.execute(); } public void updateIna(Ina a) throws SQLException { Connection connection=DBUtil.getConnection(); String sql=" "+ " update tdb_goods"+? ? ? ? " set (name=?,cate=?,brandname=?,price=?,date=?,update=current_date())"+ " where id=?"; PreparedStatement ptmt=connection.prepareStatement(sql); ptmt.setString(1, a.getName()); ptmt.setString(2, a.getCate()); ptmt.setString(3, a.getBrandname()); ptmt.setFloat(4, a.getPrice()); ptmt.setDate(5, new Date(a.getDate().getTime())); ptmt.setInt(6, a.getId()); ptmt.execute(); } public void delIna(Ina a) throws SQLException { Connection connection=DBUtil.getConnection(); String sql=" "+" delete from tdb_goods "+" where id=?"; PreparedStatement ptmt=connection.prepareStatement(sql); ptmt.setInt(1, a.getId()); ptmt.execute(); } public List<Ina> query() throws Exception{ Connection conn=DBUtil.getConnection(); System.out.println(conn); //通過數(shù)據(jù)庫的連接操作數(shù)據(jù)庫,實現(xiàn)增刪改查 Statement statement=conn.createStatement(); ResultSet resultSet=statement.executeQuery("select * from tdb_goods"); List<Ina> inas=new ArrayList<Ina>(); Ina n=null; while (resultSet.next()) { n=new Ina(); //n.setId(resultSet.getInt(id)); n.setName(resultSet.getString("name")); n.setCate(resultSet.getString("cate")); n.setBrandname(resultSet.getString("brandname")); n.setPrice((float) resultSet.getDouble("price")); n.setDate(resultSet.getDate("date")); inas.add(n); } return inas; } public Ina lookIna(Ina a) throws SQLException { Ina n=null; Connection connection=DBUtil.getConnection(); String sql=" "+" select * from tdb_goods "+" where id=?"; PreparedStatement ptmt=connection.prepareStatement(sql); ptmt.setInt(1, a.getId()); ResultSet reSet=ptmt.executeQuery(); while (reSet.next()) { n=new Ina(); n.setId(reSet.getInt("id")); n.setName(reSet.getString("name")); n.setCate(reSet.getString("cate")); n.setBrandname(reSet.getString("brandname")); n.setPrice((float) reSet.getDouble("price")); n.setDate(reSet.getDate("date"));//此處不用再進(jìn)行日期轉(zhuǎn)換,因為java.sql.Date是java.util.Date的子集 n.setCreate_date(reSet.getDate("create_date")); n.setUpdate(reSet.getDate("update")); } return n; }}
jdbc中eclipse連接MySQL問題
bilibiliniconiconi
2016-10-01 15:04:28