首页>代码>jsp+mysql开发最简单的商品增删改查的实例>/prjtest/src/com/phome/db/DbHelper.java
package com.phome.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DbHelper {
	static{
		//1.加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Connection getConnect() throws SQLException
	{
		//2.建立连接
		String url="jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8";
		Connection conn=DriverManager.getConnection(url, "root", "root");
		return conn;
	}
	
	public int executeUpdate(String sql,Object[] args)
	{
		Connection conn=null;
		PreparedStatement pst=null;
		try {
			conn=this.getConnect();
			pst=conn.prepareStatement(sql);
			if (args!=null)
			{
				for (int i = 0; i < args.length; i++) {
					pst.setObject(i+1, args[i]);
				}
			}
			int rows=pst.executeUpdate();
			return rows;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if (pst!=null)
				{
					pst.close();
				}
				if (conn!=null && !conn.isClosed())
				{
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		Connection conn=null;
		PreparedStatement pst=null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.建立连接
			String url="jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8";
			 conn=DriverManager.getConnection(url, "root", "root");
			//3.执行sql语句
			String sql="insert into product values (default,?,?,?)";
			
			 pst=conn.prepareStatement(sql);
			pst.setObject(1, "c++新品");
			pst.setObject(2, 59.3);
			pst.setString(3, "/images/indexbanner_02.gif");
			int result=pst.executeUpdate();
			
			//4.处理执行结果
			if (result>0)
			{
				System.out.println("插入数据库成功");
			}
			else
			{
				System.out.println("插入数据库失败");
			}
			//5.关闭
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if (pst!=null)
				{
					pst.close();
				}
				if (conn!=null && !conn.isClosed())
				{
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
最近下载更多
一起加油  LV4 昨天
PSSDZH  LV3 1月22日
asddwh  LV12 2023年12月25日
wdawdawda  LV1 2023年11月12日
下雨了z  LV1 2023年6月25日
ericxu1116  LV24 2023年6月14日
sdhfkh  LV4 2023年6月12日
jiuyue1  LV1 2023年6月7日
微信网友_6509411715747840  LV1 2023年6月7日
2393183118  LV1 2023年6月6日
最近浏览更多
一起加油  LV4 昨天
xinnnnn  LV1 2月28日
PSSDZH  LV3 1月22日
asddwh  LV12 2023年12月25日
ddzfgh  LV1 2023年12月25日
xiongwei11231  LV8 2023年12月21日
fff2003  LV6 2023年12月14日
李林112233  LV2 2023年12月4日
小小学代码  LV1 2023年12月2日
321170193  LV6 2023年11月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友