• Mysql测试链接


    public class GetConnectionMysql {
    	public static Connection getConnection(){
    		String driver="com.mysql.jdbc.Driver";   //获取mysql数据库的驱动类
    		String url="jdbc:mysql://133.96.46.100:3306/test"; //连接数据库(test是数据库名)
    		String name="root";//连接mysql的用户名
    		String pwd="root";//连接mysql的密码
    		Connection conn= null;
    		try {
    			Class.forName(driver);
    		} catch (ClassNotFoundException e1) {
    			System.out.println("驱动加载失败");
    			e1.printStackTrace();
    		}
    		try{
    			conn=DriverManager.getConnection(url,name,pwd);//获取连接对象
    			System.out.println("成功连接数据库!");
    			return conn;
    		}catch(SQLException e){
    			e.printStackTrace();
    			return null;
    		}finally {
    			if(conn!=null){
    				try {
    					conn.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    	
    	public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
    		try{
    			if(rs!=null){
    				rs.close();
    			}
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    		try{
    			if(ps!=null){
    				ps.close();
    			}
    		}catch(SQLException e){
    			e.printStackTrace();
    		}
    		try{
    			if(conn!=null){
    				conn.close();
    			}
    		}catch(SQLException e){
    			e.printStackTrace();	
    		}
    	}
    	
    	public static void main(String[] args) throws SQLException
    	{
    		
    		Connection cc=GetConnectionMysql.getConnection();
    	}
    }
    

      

  • 相关阅读:
    JAVA中字符串比较equals()和equalsIgnoreCase()的区别
    idea无法调试的解决方案.
    idea如何把快捷键改成eclipse的快捷键
    idea安装和破解教程
    在idea中maven项目 jar包下载不完整解决办法
    Spring boot入门级项目(超详细简易版)
    123123
    ww
    无限极操作
    无限极菜单1
  • 原文地址:https://www.cnblogs.com/zt528/p/5291890.html
Copyright © 2020-2023  润新知