Java JDBC数据库 之 PreparedStatement

http://www.verejava.com/?id=16998433130739

下载 Oracle 或者 Mysql Jar 驱动 

mysql-connector-java-3.0.10-stable-bin.jar 
ojdbc14.jar 

package com.prepared.jdbc.test;

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

public class TestAdd
{
	public static void main(String[] args)
	{
		Connection conn=null;
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "myspace", "myspace123");
			//创建一个执行 邦定 sql 语句的对象
			String sql="INSERT INTO dept(id,name)VALUES(?,?)";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			//设置 ? 的参数的值
			pstmt.setInt(1, 1);
			pstmt.setString(2, "IT");
			//执行 绑定的sql 语句
			if(pstmt.executeUpdate()>0)
			{
				System.out.println("执行成功");
			}
			else
			{
				System.out.println("执行失败");
			}
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				conn.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}





package com.prepared.jdbc.test;

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

public class TestDelete
{
	public static void main(String[] args)
	{
		Connection conn=null;
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "myspace", "myspace123");
			//创建一个执行 邦定 sql 语句的对象
			String sql="DELETE FROM dept WHERE id=?";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			//设置 ? 的参数的值
			pstmt.setInt(1, 1);
			//执行 绑定的sql 语句
			if(pstmt.executeUpdate()>0)
			{
				System.out.println("执行成功");
			}
			else
			{
				System.out.println("执行失败");
			}
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				conn.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}





package com.prepared.jdbc.test;

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

public class TestUpdate
{
	public static void main(String[] args)
	{
		Connection conn=null;
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "myspace", "myspace123");
			//创建一个执行 邦定 sql 语句的对象
			String sql="UPDATE dept SET name=? WHERE id=?";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			//设置 ? 的参数的值
			pstmt.setString(1, "行政部");
			pstmt.setInt(2, 1);
			//执行 绑定的sql 语句
			if(pstmt.executeUpdate()>0)
			{
				System.out.println("执行成功");
			}
			else
			{
				System.out.println("执行失败");
			}
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				conn.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}





package com.prepared.jdbc.test;

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

public class TestFind
{
	public static void main(String[] args)
	{
		Connection conn=null;
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "myspace", "myspace123");
			//创建一个执行 邦定 sql 语句的对象
			String sql="SELECT * FROM dept";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			
			//执行 绑定的sql 语句
			ResultSet rs=pstmt.executeQuery();
			while(rs.next())
			{
				int id=rs.getInt("id");
				String name=rs.getString("name");
				System.out.println(id+","+name);
			}
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				conn.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}

http://www.verejava.com/?id=16998433130739

posted @ 2018-06-28 08:32  verejava  阅读(162)  评论(0编辑  收藏  举报