zoukankan      html  css  js  c++  java
  • JAVA JDBC连接mysql数据库

    1、下载JDBC,下载地址

    https://dev.mysql.com/downloads/connector/j/

    3、引入包,jdk是1.8 的,选用jdbc 8.0.18 

    4、添加源代码,直接选择ZIP包,源代码自动添加,选中Driver,按住ctrl,再点击鼠标左键

    5、静态代码块

     6、连接数据库代码

    public static void main(String[] args) {
    		
    
    		//DriverManager.getConnection("jdbc:mysql://localhost:3306/netsafe&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false")
    	 Connection connection  = null;
    	 Statement statement= null;
    
    	 ResultSet resultSet =  null;
    	 //注册驱动
    		try {
    			//DriverManager.deregisterDriver(new Driver());
    			Class.forName("com.mysql.cj.jdbc.Driver");
    			connection= 	DriverManager.getConnection("jdbc:mysql://localhost:3306/8888?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "root", "root");
    			
    			statement  = connection.createStatement();
     
    			 String sqlString = "SELECT * from dept";
    			 resultSet = statement.executeQuery(sqlString);	
    		 while (resultSet.next()) {
    			String deparString = resultSet.getString("DeptNo");
    			System.out.println(deparString);
    			
    		}
    
    			
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		finally {
    			 try {
    				resultSet.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			 try {
    				statement.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			 try {
    				connection.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
           System.out.println("测试34");
    	}
    

      

  • 相关阅读:
    快速创建MockAPI
    Eolinker SaaS 7.5 版本更新
    【翻译】几个优质的REST API工具
    建立RESTful API测试程序的基础
    Ubuntu下gcc安装及使用
    c++转化成delphi的代码
    VCL组件的属性和方法详解
    Delphi组件开发教程指南目录
    FASM 第一章 简介
    (一)SQL 基础知识
  • 原文地址:https://www.cnblogs.com/topguntopgun/p/12745480.html
Copyright © 2011-2022 走看看