java代码报错,根据学习的视频一步步操作找不出问题
packagecom.java1234.jdbc.chap03;importjava.sql.Connection;importjava.sql.Statement;im...
package com.java1234.jdbc.chap03;
import java.sql.Connection;
import java.sql.Statement;
import util.DbUtil;
public class Demo2 {
private static DbUtil dbUtil=new DbUtil();
/**
* 添加图书
*
* @param bookName
* @param price
* @param author
* @param bookTypeId
* @return
* @throws Exception
*/
private static int addBook(String bookName,float price,String author,int bookTypeId)throws Exception{
Connection con=dbUtil.getcon();//获取连接
String sql="insert into t_book values(null,'"+bookName+"',"+price+","+author+","+bookTypeId+")";
Statement stmt=con.createStatement();//创建statement
int result=stmt.executeUpdate(sql);
dbUtil.close(stmt, con);//关闭statement和连接
return result;
}
public static void main(String[] args)throws Exception {
int result=addBook("linux私房菜",100,"鸟哥",1);
if(result==1){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
}
}
=========================
以下为报错信息:
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column '????“?' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at com.java1234.jdbc.chap03.Demo2.addBook(Demo2.java:25)
at com.java1234.jdbc.chap03.Demo2.main(Demo2.java:30) 展开
import java.sql.Connection;
import java.sql.Statement;
import util.DbUtil;
public class Demo2 {
private static DbUtil dbUtil=new DbUtil();
/**
* 添加图书
*
* @param bookName
* @param price
* @param author
* @param bookTypeId
* @return
* @throws Exception
*/
private static int addBook(String bookName,float price,String author,int bookTypeId)throws Exception{
Connection con=dbUtil.getcon();//获取连接
String sql="insert into t_book values(null,'"+bookName+"',"+price+","+author+","+bookTypeId+")";
Statement stmt=con.createStatement();//创建statement
int result=stmt.executeUpdate(sql);
dbUtil.close(stmt, con);//关闭statement和连接
return result;
}
public static void main(String[] args)throws Exception {
int result=addBook("linux私房菜",100,"鸟哥",1);
if(result==1){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
}
}
=========================
以下为报错信息:
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column '????“?' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at com.java1234.jdbc.chap03.Demo2.addBook(Demo2.java:25)
at com.java1234.jdbc.chap03.Demo2.main(Demo2.java:30) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询