public class Demo01 {
public static void main(String[] args) {
Demo01 demo01 = new Demo01();
try {
demo01.save(12,"电饭锅","厨具","电磁炉使用",10);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void save(int p_no,String p_name,String p_type,String p_remark,int p_num) throws ClassNotFoundException, SQLException {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.0:3306/cangku?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
conn = DriverManager.getConnection(url,"root","123456");
st = conn.createStatement();
int row = st.executeUpdate("insert into product(p_no,p_name,p_type,p_remark,p_num)"+"values('"+p_no+"','"+p_name+"','"+p_type+"','"+p_remark+"','"+p_num+"')");
System.out.println(row);
System.out.println("123");
} finally {
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
}
}
这是表
这个时student_db的表
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.SQLException;
//import java.sql.Statement;
//
//public class Demo01 {
// public static void main(String[] args) {
// Demo01 jd2 = new Demo01();
// try {
// jd2.savestudent("王萍", "男");
// } catch (ClassNotFoundException | SQLException e) {
// e.printStackTrace();
// }
// }
// public void savestudent(String name, String sex) throws ClassNotFoundException, SQLException {
// Connection connection=null;
// Statement st=null;
// try {
// Class.forName("com.mysql.cj.jdbc.Driver");
// String url= "jdbc:mysql://127.0.0.1:3306/school_db?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
// connection=DriverManager.getConnection(url,"root","123456");
// st=connection.createStatement();
// int row= st.executeUpdate("insert into t_teacher(name,sex)" +
// "values ('"+name+"','"+sex+"')");
// System.out.println(row);
// }finally {
// if(st!=null){
// st.close();
// }
// if(connection!=null){
// connection.close();
// }
// }
// }
//
//}
我用这个代码就运行成功了。