3.0JDBC学习——jdbc几种基本操作方式

JAVAWEB学习文章索引

首先说明的是不同数据库加载的驱动路径和url是不同的

这里暂时介绍mysql和sqlserver

数据库 驱动路径 url写法
mysql com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/dbname
sqlserver com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc:sqlserver://localhost:1433;DataBaseName=dbname
     
     

Statement:

例子:用Statement方式查询mysql数据库

1.获取数据库连接对象

import java.sql.*;

public class DBUtils {
	
	private static String url = "jdbc:mysql://localhost:3306/javabeantest";
	private static String name = "root";
	private static String password = "123456";
	private static Connection conn = null;
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");	
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection() {
		try {
			conn = DriverManager.getConnection(url,name,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}

2.进行查询

package com.jdbc;
import java.sql.*;
import com.dbhelp.DBUtils;
public class StatementTest {
	public static void main(String[] args) {
		Connection conn = DBUtils.getConnection();
		try {
			Statement sttm = conn.createStatement();
			ResultSet rs = sttm.executeQuery("select * from person");
			while(rs.next()) {
				System.out.println(rs.getObject(1)+".."+rs.getObject(2)+".."+rs.getObject(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

3进行插入

public static int insert() {
		int r = 0;
		String sql = "insert into person(id,name,age) values(4,'李明',22)";
		Connection conn = DBUtils.getConnection();
		try {
			r = conn.createStatement().executeUpdate(sql);
			System.out.println(r);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return r;
	}

4进行修改

public static int update() {
		int r = 0;
		String sql = "update person set age=18 where id = 4";
		Connection conn = DBUtils.getConnection();
		try {
			r = conn.createStatement().executeUpdate(sql);
			System.out.println(r);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

5进行删除

public static int delete() {
		int r = 0;
		String sql = "delete from person where id = 4";
		Connection conn = DBUtils.getConnection();
		try {
			r = conn.createStatement().executeUpdate(sql);
			System.out.println(r);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return r;
	}

使用PreparedStatement

查询:

public static void sellectAll() {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from person";
		try {
			ResultSet rs = conn.prepareStatement(sql).executeQuery();
			while(rs.next()) {
				System.out.println(rs.getObject(1)+".."+rs.getObject(2)+".."+rs.getObject(3));
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

插入:

public static int insert() {
		int r = 0;
		String sql = "insert into person(id,name,age) values(?,?,?)";
		Connection conn = DBUtils.getConnection();
		try {
			PreparedStatement ppst = conn.prepareStatement(sql);
			ppst.setInt(1, 4);
			ppst.setString(2, "李明");
			ppst.setInt(3, 22);
			r = ppst.executeUpdate();
			System.out.println(r);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return r;
	}

更新:

public static int update() {
		int r = 0;
		String sql = "update person set age=? where id = ?";
		Connection conn = DBUtils.getConnection();
		try {
			PreparedStatement ppst = conn.prepareStatement(sql);
			ppst.setInt(1, 18);
			ppst.setInt(2, 4);
			r = ppst.executeUpdate();
			System.out.println(r);
			ppst.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return r;
	}

删除:

public static int delete() {
		int r = 0;
		String sql = "delete from person where id = 4";
		Connection conn = DBUtils.getConnection();
		try {
			PreparedStatement ppst = conn.prepareStatement(sql);
			r = ppst.executeUpdate();
			System.out.println(r);
			ppst.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return r;
	}

CallableStatement:

首先创建一个存储过程

delimiter//
	create procedure coun(out s int)
		begin
		select count(*) into s from person;
		end//
delimiter;

例子:

public static void main(String[] args) {
	
		CallableStatement cstmt = null;
		Connection conn = DBUtils.getConnection();
		String sql = "call coun(?)";
		try {
			//使用conn来创建一个CallableStatement对象
			cstmt = conn.prepareCall(sql);
			//注册第1个参数为int类型
			cstmt.registerOutParameter(1, Types.INTEGER);
			//执行语句
			cstmt.execute();
			System.out.println("结果为:"+cstmt.getInt(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

你可能感兴趣的:(javaweb)