Java 1203 JDBC

package com.lovo;

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

/**
 * 数据库
 * @author Dean
 *
 */

public class Test01 {
	public static void main(String[] args) {
		Connection con = null;
		// 1.加载驱动程序
		try {
			Class.forName("com.mysql.jdbc.Driver");
			// 2.创建连接;
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/company", 
					"root", "123456");
			System.out.println(con + ":连接成功");
			// 3.创建语句对象
			Statement stmt = con.createStatement();
			// 4.发出SQL语句得到结果集(游标)
			ResultSet rs = stmt.executeQuery("select * from emp");
			// 5.循环遍历结果集
			while(rs.next()){  //先判断游标是否可以取到下一条记录
				System.out.println("姓名:" + rs.getString("ename"));
				System.out.println("工资:" + rs.getInt("sal"));
				System.out.println("=====分割线=====");
			}
			
			//更新一条数据
			int temp = stmt.executeUpdate("Update emp set ename ='曾雯' where eno=4466");
			System.out.println(temp);
		    //删除一条数据	
			int temp1 = stmt.executeUpdate("Delete from emp  where eno=4466");
			System.out.println(temp1);
			//添加一条数据
			int temp2 = stmt.executeUpdate("insert into emp (eno,ename,job,mgr,sal,comm,dno) "
					+ "values(4466,'黄佳梦','会计',5566,4000,300,10)");
			System.out.println(temp2);
			
			//批量加入数据
			stmt.addBatch("insert into emp values(4411,'龙哥','会计',5566,3000,300,10)");
			stmt.addBatch("insert into emp values(4422,'梦姐','会计',5566,3100,400,10)");
			stmt.addBatch("insert into emp values(4433,'炮娘','会计',5566,3200,100,10)");
			int[] count = stmt.executeBatch();
			for(int i=0; i<count.length; i++){
				System.out.println("第" + (i+1) + "条语句影响的行数为:" + count[i] );
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			if(con != null){
				try {
					con.close();  // 6.关闭连接
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值