⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preparestatementtest.java

📁 Java与数据库的连接, 链接创建
💻 JAVA
字号:
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 
 */

/**
 * @author Administrator
 * 
 */
public class PrepareStateMentTest {

	/**
	 * ORACLE 链接驱动
	 */
	public static String DRIVERNAME = "oracle.jdbc.driver.OracleDriver";

	/**
	 * 链接
	 */
	private Connection conn = null;

	/**
	 * 控制函数
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		PrepareStateMentTest t = new PrepareStateMentTest();
		try {
			// 链接创建
			t.initConn();

			// 数据插入
			t.insertTable();
			// 数据检索
			t.selectTable();

			// 数据更新
			t.updateTable();
			// 数据检索
			t.selectTable();

			// 数据删除
			t.deleteTable();
			// 数据检索
			t.selectTable();

			t.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				t.conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			// 链接删除
			try {
				t.closeConn();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 链接关闭
	 * 
	 * @throws SQLException
	 */
	private void closeConn() throws SQLException {
		this.conn.close();
	}

	/**
	 * 链接初始化
	 * 
	 * @throws Exception
	 */
	private void initConn() throws Exception {

		Class.forName(DRIVERNAME);
		String url = "jdbc:oracle:thin:@192.168.1.103:1521:training";
		this.conn = DriverManager.getConnection(url, "train", "train");
		this.conn.setAutoCommit(false);
	}

	/**
	 * 数据插入
	 * 
	 * @throws Exception
	 */
	public void insertTable() throws Exception {
		String sql = null;
		PreparedStatement sp = null;
		try {
			sql = "INSERT INTO Student(name, sex, address, birthday) "
					+ "VALUES (?, ?, ?, ?)";
			sp = conn.prepareStatement(sql);
			sp.setString(1, "lisi");
			sp.setInt(2, 0);
			sp.setString(3, "NJ");
			sp.setDate(4, Date.valueOf("2000/01/01"));

			int count = sp.executeUpdate();
			System.out.println(count);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 数据删除
	 * 
	 * @throws Exception
	 */
	public void deleteTable() throws Exception {
		String sql = null;
		PreparedStatement sp = null;
		try {
			sql = "DELETE FROM Student WHERE name = ? ";
			sp = conn.prepareStatement(sql);
			sp.setString(1, "lisi");
			int count = sp.executeUpdate();
			System.out.println(count);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 数据更新
	 * 
	 * @throws Exception
	 */
	public void updateTable() throws Exception {
		String sql = null;
		PreparedStatement sp = null;
		try {
			sql = "UPDATE Student SET sex = ?, address= ?, birthday = ? "
					+ "WHERE name = ?)";
			sp = conn.prepareStatement(sql);
			sp.setInt(1, 1);
			sp.setString(2, "SH");
			sp.setDate(3, Date.valueOf("2001/02/02"));
			sp.setString(4, "lisi");
			int count = sp.executeUpdate();
			System.out.println(count);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 数据检索
	 * 
	 * @throws Exception
	 */
	public void selectTable() throws Exception {
		String sql = null;
		PreparedStatement sp = null;
		try {
			sql = "SELECT * FROM Student WHERE name=? ";
			sp = conn.prepareStatement(sql);
			sp.setString(1, "lisi");
			ResultSet rs = sp.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getString("name"));
				System.out.println(rs.getInt("sex"));
				System.out.println(rs.getString("address"));
				System.out.println(rs.getDate("birthday"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -