原生JDBC

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38323645/article/details/88842453

1 未预编译的增删改查

//未预编译的增删改查
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DML_DQL1test {
	static void add(){
		Connection con = null;//连接对象
		Statement st = null;//语句对象
		try{
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			//连接数据库               					   jdbc:mysql://<hostname>[<:3306>]/<dbname>
			con = DriverManager.getConnection("jdbc:mysql://192.168.16.241:3306/mytest", "root", "root");
			st = con.createStatement();//创建语句对象
			//增删改(用.executeUpdate)返回的int数据(受影响条数)--操作数据库
			int i = st.executeUpdate("insert into student(sname,gender,classid) values('赵云','女','3')");
			if(i>0){
				System.out.println("成功");
			}else{
				System.out.println("失败");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if (st != null)
					st.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	static void select(){//查
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;//结果集对象
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://192.168.16.241:3306/mytest", "root", "root");
			st = con.createStatement();
			//查(用.executeQuery)--操作数据库
			rs = st.executeQuery("select * from student where sid=5");
			if(rs.next()){
				System.out.print("编号:"+rs.getInt("sid")+" ");
				System.out.print("姓名:"+rs.getString("sname")+" ");
				System.out.print("性别:"+rs.getString("gender")+" ");
				System.out.print("班级:"+rs.getInt("classid")+" ");
				System.out.print("分数:"+rs.getInt("score")+" ");
				System.out.println("课程:"+rs.getString("cusion")+" ");
			}
			rs = st.executeQuery("select sname as aa from student where sid=5");
			if(rs.next()){
				System.out.println("姓名:"+rs.getString("aa"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if (rs != null)
					rs.close();
				if (st != null)
					st.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		add();
		select();
	}
}

2 有预编译的增删改查

//预编译的增删改查
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DML_DQL2test {
	static void update(){//增删改
		Connection con = null;//连接对象
		PreparedStatement pst = null;//预编译语句对象
		try{
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			//链接数据库
			con = DriverManager.getConnection("jdbc:mysql://192.168.16.241:3306/mytest", "root", "root");
			//创建预编译语句对象
			pst = con.prepareStatement("insert into student(sname,gender,classid) values(?,?,?)");
			pst.setString(1, "小黄第");//下标从1开始
			pst.setString(2,"男");
			pst.setInt(3,5);
			int i =pst.executeUpdate();//操作数据库 增删改
			System.out.println(i>0?"成功":"失败");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if (pst != null)
					pst.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	static void select(){//查
		Connection con = null;//连接对象
		PreparedStatement pst = null;//预编译语句对象
		ResultSet rs = null;
		try{
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			//链接数据库
			con = DriverManager.getConnection("jdbc:mysql://192.168.16.241:3306/mytest", "root", "root");
			//创建预编译语句对象
			pst = con.prepareStatement("select * from student ");
			rs = pst.executeQuery();//执行查(用.executeQuery)--操作数据库
			while(rs.next()){
				System.out.print("编号:"+rs.getInt("sid")+" ");
				System.out.print("姓名:"+rs.getString("sname")+" ");
				System.out.print("性别:"+rs.getString("gender")+" ");
				System.out.print("班级:"+rs.getInt("classid")+" ");
				System.out.print("分数:"+rs.getInt("score")+" ");
				System.out.println("课程:"+rs.getString("cusion")+" ");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if (pst != null)
					pst.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		//update();
		select();
	}
}

3 封装


//增删改查的封装
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class myDB{//工具类
	public static final String driver="com.mysql.jdbc.Driver";//
	public static final String url="jdbc:mysql://192.168.9.171:3306/test";
	public static final String user="root";
	public static final String password="root";
	Connection co=null;//连接对象
	PreparedStatement pst=null;//预编译语句对象
	ResultSet rs=null;//结果集对象
	public Connection getco(){//加载驱动和链接数据库的方法   返回的是连接对象co
		try{
			Class.forName(driver);//加载驱动
			co=DriverManager.getConnection(url, user, password);//连接数据库
		}catch(Exception e){
			e.printStackTrace();
		}
		return co;
	}
	public void closeAll(){//关闭
		try{
			if(rs!=null) rs.close();//关闭结果集对象
			if(pst!=null) pst.close();//关闭预编译语句对象
			if(co!=null) co.close();//关闭连接对象
		}catch(Exception e){
			e.printStackTrace();
		}
	} 
	public int update(String sql,Object...obj){//增删改  返回受影响条数、 int型
		int  result=0;
		try{
			co=getco();//调用 加载驱动器和链接数据库的方法
			pst=co.prepareStatement(sql);//预编译语句对象
			if(obj!=null){
				for(int i=0;i<obj.length;i++){
					pst.setObject(i+1, obj[i]);//下标从1开始
				}
			}
			result=pst.executeUpdate();//执行SQL语句 增删改
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			closeAll();//调用关闭的方法
		}
		return result;
	}
	public ResultSet select(String sql,Object...obj){//查 返回 结果集对象 rs
		try{
			co=getco();//调用 加载驱动器和链接数据库的方法
			pst=co.prepareStatement(sql);//预编译语句对象
			if(obj!=null){
				for(int i=0;i<obj.length;i++){
				pst.setObject(i+1, obj[i]);	//下标从1开始
				}
			}
			rs=pst.executeQuery();//执行SQL语句 查
		}catch(Exception e){
			e.printStackTrace();
		}
		return rs;
	}
}
import java.sql.ResultSet;
public class test {
public static void main(String[] args) {
		myDB db = new myDB();
		
		int i1=db.update("insert into student(name,gender,score) value (?,?,?)" , "小黑","女",75);
		System.out.println(i1>0?"成功":"失败");  //增数据
		int i2=db.update("update student set name=? where id=?", "小白",1201232065);
		System.out.println(i2>0?"成功":"失败");  //改数据
		int i3=db.update("delete from student where id=? or id=?", 1,2);
		System.out.println(i3>0?"成功":"失败");//删数据
		
		try {//查询数据
			//调用myDB中的select方法
			ResultSet rs = db.select("select * from student where id=?",1201232064);
			while (rs.next()) {
				System.out.println(rs.getString("name")+" "+rs.getInt("score"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.closeAll();//查询完 关闭
		}
	}
}


猜你喜欢

转载自blog.csdn.net/weixin_38323645/article/details/88842453
今日推荐