添加失败的原因是啥啊?
是导报的错误么?还是调用的问题?
还是说我的这两个·包的代码没有关联所以添加失败?
两个main方法分别执行能插入成功才怪!
那就只能是一种情况,你的代码有问题或者数据库通讯不正常
public class Main {
public static void main(String[] args) throws SQLException {
Connection con = getCon();
Statement statement = con.createStatement();
boolean execute = statement.execute("select * from menu");
if(execute){
System.out.println("sql操作成功");
}else {
System.out.println("sql操作失败");
}
}
public static Connection getCon(){
try {
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/my";
String username = "root";
String pwd = "123456";
Connection connection;
try {
connection = DriverManager.getConnection(url, username, pwd);
System.out.println("连接成功");
return connection;
} catch (SQLException e) {
System.out.println("连接失败");
return null;
}
}
}
贴一下报错信息,是不是sql没写对