12.28

package com.neu;

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

public class Test0 {

	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		// TODO Auto-generated method stub
//		Scanner input = new Scanner(System.in);
//		System.out.print("请输入用户名:");
//		String username = input.nextLine();
//		System.out.println("请输入密码:");
//		String password = input.nextLine();
		
		Class.forName("com.mysql.jdbc.Driver");
		//ctrl+2 L
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		
		//编写sql语句
		String sql = "select count(*) from stdb where sex = '女' and score>80";
		
		
		//基于当前连接,得到一个执行sql语句的命令对象
//		Statement statement = connection.createStatement();
		PreparedStatement statement = connection.prepareStatement(sql);		
//		statement.setString(1, username);
//		statement.setString(2, password);

		//执行查询语句,返回结果集
		ResultSet rs = statement.executeQuery();
		
		//该方法读取结果集中的一条记录,如果得到下一条记录,返回true,如果没有下一条记录,返回false
		while(rs.next()) {
			
			System.out.println(rs.getInt(1));
		}
		
		rs.close();
		
		statement.close();		
		
		//关闭连接,使用完马上关闭,释放资源
		connection.close();
	}
	@org.junit.Test
	public void testUpdate() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		//ctrl+2 L
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
		//编写sql语句
		String sql = "update stdb set score = 100 where name like '张%' and sex='男'";
		//基于当前连接,得到一个执行sql语句的命令对象
		PreparedStatement statement = connection.prepareStatement(sql);
		//执行sql语句,该方法一般用来执行增删改操作,返回值表示影响数据库的行数
		int n = statement.executeUpdate();
		
		if(n == 1) {
			System.out.println("执行成功!");
		}else {
			System.out.println("执行失败!");
		}
		
		//关闭连接,使用完马上关闭,释放资源
		connection.close();
	}
	@org.junit.Test
	public void testQuery() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		//ctrl+2 L
		Connection connection = 
				DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
		//编写sql语句
		String sql = "select name,sex,score from stdb where sex='女' and score>60 ";
		//基于当前连接,得到一个执行sql语句的命令对象
		PreparedStatement statement = connection.prepareStatement(sql);
		//执行sql语句,该方法一般用来执行增删改操作,返回值表示影响数据库的行数
//		int n = statement.executeUpdate(sql);
		//执行查询语句,返回结果集
		ResultSet rs = statement.executeQuery();
		
		//该方法读取结果集中的一条记录,如果得到下一条记录,返回true,如果没有下一条记录,返回false
		while(rs.next()) {
			System.out.println(rs.getString(1));
			System.out.print(rs.getString(2)+"\t");
			System.out.print(rs.getInt(3)+"\t");
			System.out.println();
			
		}
		
		
		
		
		
		rs.close();//关闭结果集
		statement.close();//关闭命令
		
		
		//关闭连接,使用完马上关闭,释放资源
		connection.close();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值