参数为float类型的,不加单引号'';
float num =1;
String name="张三";
insert into tb_table(bnum,name) values('"+bnum+"','"+name+"');(错误)
insert into tb_table(bnum,name) values("+bnum+",'"+name+"');(正确)
JDBC操作封装
package com.ht.tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///shop", "root", "226998");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public void close() {
try {
if (rs != null) {
rs.close();
}
if(stmt!=null) {
stmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
ConnDB conn = new ConnDB();
String sql = "select * from user";
ResultSet rs = conn.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("name") + "---" + rs.getString("email"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}