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

    jdbc开车到官网下载,上jdk相关的文件夹或更少。要么jar添加的文件project以下

    package test_mysql;
    import java.sql.*;
    import java.util.Set;
    public class testjdbc {
    	public static Connection getConnection() throws ClassNotFoundException, SQLException{
    		String URL="jdbc:mysql://localhost:3306/employees";
    		//载入驱动程序
    		Class.forName("com.mysql.jdbc.Driver");
    		String user="root";
    		String passwd="";
    		//默认密码为空
    		//还有选择数据库,哪个数据
    		//DriverManager.
    		//Connection con=DriverManager.getConnection(URL, user, passwd);
    		//Connection con=DriverManager.getConnection(url, user, password);
    		String test_url="";
    		//String my_url="jdbc:mysql://localhost:3306;DatabaseName=employees;User=root;Password=";
    		//test_url+=URL+3306+";"+"DatabaseName=employees;"+"User=root;"+"Password="+"";
    		Connection con=DriverManager.getConnection(URL,user,passwd);
    		return con;
    	}
    	public static void main(String[] args){
    		String sql="select * from  departments";
    		Statement statement=null;
    		Connection con=null;
    		ResultSet set=null;
    		try {
    			con = getConnection();
    			//con.set
    			if(con==null)
    				System.out.println("con null");
    			statement=con.createStatement();
    			set=statement.executeQuery(sql);
    			//int n=set.getFetchSize();
    			//System.out.println(n);
    			while(set.next()){
    				System.out.println(set.getString(1)+"  "+set.getString(2));
    			}
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    	  if(con!=null)
    		try {
    			set.close();
    			statement.close();
    			con.close();
    			
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		}
    		
    	}
    }


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    树莓派使用记录 修改国内软件源《二》
    树莓派使用记录 安装系统《一》
    C# 委托 Action 实现代码执行时间日志记录
    微软 Visual Studio 离线下载
    项目框架搭建工具
    WebApi 重写 DefaultHttpControllerSelector 实现路由重定向
    开发相关网页收藏
    造SQL语句
    报错:Every derived table must have its own alias
    html
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4631196.html
Copyright © 2011-2022 走看看