亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jdbc中eclipse連接MySQL問題

jdbc中eclipse連接MySQL問題

bilibiliniconiconi 2016-10-01 15:04:28
?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類,調用Dao層方法,進行具體的數據操作,視圖層?*/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層封裝增刪改查方法,業務邏輯層?*/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()));//傳進來的是java.util類型的date,但需要的是java.sql類型,所以要進行類型轉換 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); //通過數據庫的連接操作數據庫,實現增刪改查 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"));//此處不用再進行日期轉換,因為java.sql.Date是java.util.Date的子集 n.setCreate_date(reSet.getDate("create_date")); n.setUpdate(reSet.getDate("update")); } return n; }}
查看完整描述

2 回答

  • 2 回答
  • 1 關注
  • 4962 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號