求解:java与mysql连接是插入不进去,不执行executeupdate方法,下面为我的代码及错误
importjava.sql.Connection;importjava.sql.Date;importjava.sql.DriverManager;importjava...
import java.sql.Connection;import java.sql.Date;import java.sql.DriverManager;import java.sql.PreparedStatement;
import java.sql.SQLException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;public class UserDao { public static Connection conn(){ Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK", "root", "root"); } catch (SQLException e) { e.printStackTrace(); } return con; } public static Boolean insert(){ boolean ret = false; Connection con = conn(); PreparedStatement st = null; Calendar cd=Calendar.getInstance(); Date date = new Date(System.currentTimeMillis()); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { st = con.prepareStatement("insert into tb_counts(id,counts,last_ip,last_date) values((SELECT MAX(id) FROM tb_counts)+1,?,?,'"+dateFormat.format(date)+"')"); st.setInt(1,6); st.setString(2,"wer"); int i=st.executeUpdate(); if(i>0){ ret = true; }else{ ret=false; } } catch (SQLException e) { e.printStackTrace(); } return ret; }} 展开
import java.sql.SQLException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;public class UserDao { public static Connection conn(){ Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK", "root", "root"); } catch (SQLException e) { e.printStackTrace(); } return con; } public static Boolean insert(){ boolean ret = false; Connection con = conn(); PreparedStatement st = null; Calendar cd=Calendar.getInstance(); Date date = new Date(System.currentTimeMillis()); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { st = con.prepareStatement("insert into tb_counts(id,counts,last_ip,last_date) values((SELECT MAX(id) FROM tb_counts)+1,?,?,'"+dateFormat.format(date)+"')"); st.setInt(1,6); st.setString(2,"wer"); int i=st.executeUpdate(); if(i>0){ ret = true; }else{ ret=false; } } catch (SQLException e) { e.printStackTrace(); } return ret; }} 展开
2个回答
展开全部
public static Boolean insert(){
boolean ret = false;
Connection con = conn();
PreparedStatement st = null;
Calendar cd=Calendar.getInstance();
Date date = new Date(System.currentTimeMillis());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Statement stmt=con.createStatement();
ResultSet rst=stmt.excueteQuery(SELECT MAX(id) as id1 FROM tb_counts);
int i=rst.getInt("id1")+1;
rst.close();
stmt.close();
st = con.prepareStatement("insert into tb_counts(id,counts,last_ip,last_date) values(?,?,?,'"+dateFormat.format(date)+"')");
st.setInt(1,i);
st.setInt(2,6);
st.setString(3,"wer");
int i=st.executeUpdate();
if(i>0){
ret = true;
}else{
ret=false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
boolean ret = false;
Connection con = conn();
PreparedStatement st = null;
Calendar cd=Calendar.getInstance();
Date date = new Date(System.currentTimeMillis());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Statement stmt=con.createStatement();
ResultSet rst=stmt.excueteQuery(SELECT MAX(id) as id1 FROM tb_counts);
int i=rst.getInt("id1")+1;
rst.close();
stmt.close();
st = con.prepareStatement("insert into tb_counts(id,counts,last_ip,last_date) values(?,?,?,'"+dateFormat.format(date)+"')");
st.setInt(1,i);
st.setInt(2,6);
st.setString(3,"wer");
int i=st.executeUpdate();
if(i>0){
ret = true;
}else{
ret=false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询