vlambda博客
学习文章列表

Ajax完整项目---数据添加和修改

效果演示


添加数据


修改数据


项目结构




SqlConnect.java部分

package sqlconnect;
import java.sql.*;public class SqlConnect { static private Connection conn = null; static private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=ShopSystem"; static private String name = "sa"; static private String passwd = "sa"; public static Connection connection() { try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn = DriverManager.getConnection(url,name,passwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void unconnection(ResultSet rst,PreparedStatement pst,Connection conn) { if (rst != null) { try { rst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (pst != null) { try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}


AddInfoDao.java部分

package addinfodao;
import java.sql.*;
import sqlconnect.SqlConnect;public class AddInfoDao { static private Connection conn = null; static private PreparedStatement pst = null; public static boolean addinfo(String type,String id,String name,String price, String quantity,String image,String description,String time) { boolean result = true; try { conn = SqlConnect.connection(); pst = conn.prepareStatement("insert into Product values(?,?,?,?,?,?,?,?)"); pst.setString(1, type); pst.setString(2, id); pst.setString(3, name); pst.setString(4, price); pst.setString(5, quantity); pst.setString(6, image); pst.setString(7, description); pst.setString(8, time); pst.executeUpdate(); result = true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); result = false; }finally { if (pst != null) { try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return result; }}


因为代码比较多在这就不一一贴出来了,完整项目文件,数据库驱动和数据库可以回复【ajax完整案例】获取