java代码, eclipse连接数据库

java代码, eclipse连接数据库


eclipse 连接 MySQL 数据库的基本操作
  • 1.jar文件
    java代码要想操作数据库需要引入jar文件
    jar文件是eclipse连接数据库的驱动文件.
    jar文件可在网上自行搜索下载.
jar包下载地址:https://mvnrepository.com
  • 2 eclipse中导入jar包
    eclipse中新建项目,直接把jar包复制到项目中
    在这里插入图片描述
然后对jar包进行解压操作

在这里插入图片描述

然后项目里新建class文件.
3.加载驱动
> 主函数里编写:Class.forName("com.mysql.jdbc.Driver"); 
4.建立连接
获取数据库连接对象
        String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";   //url jdbc有自己的写法  jdbc:oracl
		String user="root";
		String password="1234";
		conn = DriverManager.getConnection(url, user, password);
实现数据库的操作
   1.编写sql语句
     String query_sql="select*from commoditytype";
		
   2.获取执行SQL语句的对象
       Statement state = conn.createStatement();	
   
   3.执行SQL语句,实现查询
		ResultSet  rs=state.executeQuery(query_sql);

	注意:要记得关闭连接

示例:

public static void main(String[] args) {
		Connection conn = null;
		try {
			// 1.加载MySQL的驱动类  com.mysql.jdbc.Driver
			/*
			 * 扩展问题 JVM什么时候将类加载到内存中?
			 */
			Class.forName("com.mysql.jdbc.Driver");
			// 2.获取数据库连接对象
			// 。连接数据库需要哪些要素
			//   host port user password
			String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";   //url jdbc有自己的写法  jdbc:oracl
			String user="root";
			String password="1234";
			conn = DriverManager.getConnection(url, user, password);
			
			//TODO  3.实现数据库的操作
			//  3.1 编写sql语句
			//String insert_sql = "insert into commoditytype (ct_id,ct_name) values (8,'电脑配件')";
			//String update_sql = "update commoditytype set ct_name=电脑保护软件";//  3.2 获取执行对象
			//String delete_sql="delete from commoditytype where ct_id=8";
			String query_sql="select*from commoditytype";
			Statement state = conn.createStatement();
			//  3.3.1执行SQL命令,IUD返回受影响的行数
			//int s = state.executeUpdate(insert_sql);
			//int s=state.executeUpdate(delete_sql);
			// System.out.println(s);
			//  3.3.2执行SQL语句,实现查询
			ResultSet  rs=state.executeQuery(query_sql);
			while(rs.next()) {
				//迭代数据集合中的每一行,从每一行中获取相应的数据
				String cu_id=rs.getString(1);
				String cu_name=rs.getString(2);
				String cu_phone=rs.getString(3);
                int  cu_gender=rs.getInt(4);
                String cu_address=rs.getString(5);
                System.out.print(cu_id+":"+cu_name+"|"+cu_phone+"|"+cu_gender+"|"+cu_address);
                
                //。也可以根据字段名称来获取
                //rs.getString("cu_id");//这个和上面那个获取的是同一个值,等同于rs.getString(1)
                System.out.println();
                
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();//。关闭数据库连接对象
				}  
			}	
		}
	}
  • 除了查询语句之外,其他的修改,删除,添加语句返回值都为int类型,表示数据库中执行此命令后几行受到影响
  • 连接MySQL数据库用到DriverManager类,通过DriverManager类中的getConnection()方法可以建立连接,则经连接返回,否则抛出SQLException异常。

模仿登录

public static void main(String[] args) {
		Scanner  scanner=new Scanner(System.in);
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";   //url jdbc有自己的写法  jdbc:oracl
			String user="root";
			String password="1234";
			conn = DriverManager.getConnection(url, user, password);
			
			//Scanner input = new Scanner(System.in);
			//System.out.println("请输入用户名称:");
			//String cu_id=scanner.next();
			// sql注入攻击
			String cu_name="' or 1=1 #";
			//System.out.println("请输入用户手机号:");
			String cu_phone="dadadadad";
			
			String sql = "select count(*) from customer where cu_name='"+cu_name+"' and cu_phone='"+cu_phone+"'";
			Statement state = conn.createStatement();
			ResultSet rs = state.executeQuery(sql);
			rs.next();
			int count=rs.getInt(1);
			if(count>0) {
				System.out.println("登录成功!");
			}else {
				System.out.println("用户名或手机号错误,登录失败!");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					
				}
			}
		}
	}

如何实现查询 来解决注入攻击

public static void main(String[] args) {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";   //url jdbc有自己的写法  jdbc:oracl
			String user="root";
			String password="1234";
			conn = DriverManager.getConnection(url, user, password);
			/*
			 * // String cu_name="' or 1=1 #"; // String cu_phone="dadadadad";
			 * 
			 * String cu_name="刘德华"; String cu_phone="7777";
			 * 
			 * String sql = "select count(*) from customer where cu_name=? and cu_phone=?";
			 * 
			 * PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, cu_name);
			 * ps.setString(2, cu_phone);
			 * 
			 * ResultSet rs = ps.executeQuery(); rs.next();
			 * System.out.println(rs.getInt(1));
			 */
			
			// update该刘德华的密码 7777 -> 9999
			String cu_id = "2a4e0cdd-380e-41bf-960f-f2f6d0a6ccae";
			String cu_phone="9999";
			String sql = "update customer set cu_phone=? where cu_id=?";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, cu_phone);
			ps.setString(2, cu_id);
			
			int line = ps.executeUpdate();
			
			System.out.println(line);
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用Eclipse连接数据库的步骤: 1. 下载并安装数据库驱动程序:根据你所使用的数据库类型,下载相应的数据库驱动程序。将驱动程序的JAR文件复制到Eclipse项目的lib文件夹中。 2. 在Eclipse中创建一个Java项目:在Eclipse中选择File -> New -> Java Project,然后输入项目名称并点击Finish。 3. 添加数据库驱动程序到项目的构建路径:右键点击项目,选择Properties -> Java Build Path -> Libraries -> Add JARs,然后选择之前复制到lib文件夹中的数据库驱动程序JAR文件。 4. 创建数据库连接:在Eclipse中选择Window -> Show View -> Other,然后在弹出的窗口中选择Database -> Database Connections。在Database Development视图中,右键点击Database Connections,选择New -> Database Connection。 5. 配置数据库连接:在弹出的窗口中,选择你所使用的数据库类型,并填写相应的连接信息,如数据库URL、用户名和密码等。点击Test Connection按钮来测试连接是否成功。 6. 连接数据库:在Eclipse中选择Window -> Show View -> Other,然后在弹出的窗口中选择Database -> Database Explorer。在Database Development视图中,右键点击Database Connections,选择Connect。 7. 执行数据库操作:在Database Explorer中,你可以查看数据库的表、执行SQL查询、插入、更新和删除数据等操作。 请注意,以上步骤是一般的连接数据库的过程,具体步骤可能会因为使用的数据库类型和版本而有所不同。确保你已经正确安装了数据库驱动程序,并且填写了正确的连接信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值