为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 4923 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信