PreparedStatement语句插入数据

 代码:

 1.直接调用插入:

DButil.java

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.Connection;

public class DButil {
		/*
		 * 打开数据库
		 */
	    private static String driver;//连接数据库的驱动
	    private static String url;
	    private static String username;
	    private static String password;
	    
	    static {
	    	driver="com.mysql.jdbc.Driver";//需要的数据库驱动
	    	url="jdbc:mysql://localhost:3306/test";//数据库名路径
	    	username="root";
	    	password="root";
	    }
		public static Connection open()
		{
			try {
				Class.forName(driver);
				return (Connection) DriverManager.getConnection(url,username, password);
			} catch (Exception e) {
				System.out.println("数据库连接失败!");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//加载驱动

			return null;
		}
        
		/*
		 * 关闭数据库
		 */
		public static void close(Connection conn)
		{
			if(conn!=null)
			{
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

}

jdbcconnection.java

import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class jdbcconnection {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       insert("yangxu","yangxu@qq.com");
	}

	static void insert(String name,String email)
	{
		String sql="insert into Haige(name,email) value(?,?)";
		Connection conn=DButil.open();
		try {
			PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1,name);
			pstmt.setString(2,email);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DButil.close(conn);
		}
		
	}
}

2.面向对象的方式

Customer.java

public class Customer {
 int id;
 String name;
 String email;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getEmail() {
	return email;
}
public void setEmail(String email) {
	this.email = email;
}

}

jdbcconnection.java(插入数据)

import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class jdbcconnection {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       //insert("yangxu","yangxu@qq.com");
		Customer c=new Customer();
		c.setName("zhangbing");
		c.setEmail("zhangbing@qq.com");
		insert(c);
	}

	static void insert(Customer c)
	{
		String sql="insert into Haige(name,email) value(?,?)";
		Connection conn=DButil.open();
		try {
			PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1,c.getName());
			pstmt.setString(2,c.getEmail());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DButil.close(conn);
		}
		
	}
}

jdbcconnection.java(修改数据)

import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class jdbcconnection {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       //insert("yangxu","yangxu@qq.com");
		Customer c=new Customer();
//		c.setName("zhangbing");
//		c.setEmail("zhangbing@qq.com");
		//insert(c);
		c.setId(1001);
		c.setName("kaixin");
		Update(c);
	}

	static void Update(Customer c)
	{
		String sql="update haige set name=? where id=?";
		Connection conn=DButil.open();
		try {
			PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1,c.getName());
			pstmt.setInt(2,c.getId());;
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DButil.close(conn);
		}
		
	}
}

jdbcconnection.java(删除数据)

import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class jdbcconnection {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       //insert("yangxu","yangxu@qq.com");
//		Customer c=new Customer();
		c.setName("zhangbing");
		c.setEmail("zhangbing@qq.com");
//		//insert(c);
//		c.setId(1001);
//		c.setName("kaixin");
//		Update(c);
		delete(1006);
	}

	static void delete(int id)
	{
		String sql="delete from haige where id=?";
		Connection conn=DButil.open();
		try {
			PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
			pstmt.setInt(1,id);;
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DButil.close(conn);
		}
		
	}
}

jdbcconnection.java(查询数据)

import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class jdbcconnection {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       //insert("yangxu","yangxu@qq.com");
//		Customer c=new Customer();
		c.setName("zhangbing");
		c.setEmail("zhangbing@qq.com");
//		//insert(c);
//		c.setId(1001);
//		c.setName("kaixin");
//		Update(c);
		//delete(1006);
		Customer c=query(1005);
		System.out.println(c.getId()+","+c.getName()+","+c.getEmail());
	}

	static Customer query(int id)
	{
		String sql="select * from haige where id=?";
		Connection conn=DButil.open();
		try {
			PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
			pstmt.setInt(1,id);
			ResultSet rs=pstmt.executeQuery();
		    if(rs.next())
		    {
		    	String name=rs.getString(2);
		    	String email=rs.getString(3);
		    	Customer c=new Customer();
		    	c.setId(id);
		    	c.setName(name);
		    	c.setEmail(email);
		    	return c;
		    }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DButil.close(conn);
		}
		return null;
	}
}


知识点总结:

PreparedStatement 只能静态操作SQL语句,PreparedStatement 通过使用静态符“?”,来预生成SQL语句,从而达到动态操作的功能

  • 10
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值