测试连接数据库

来源:互联网 发布:什么叫javascript对象 编辑:程序博客网 时间:2024/06/05 10:17
public class GetConnection {       public Connection getConnection()         {             Connection conn=null;             try             {                 try {                     Class.forName("com.mysql.jdbc.Driver");                 }                 catch (ClassNotFoundException ex)                 {                     System.out.println("加载驱动程序有错误");                 }                 String url = "jdbc:mysql://120.24.91.184:3306/oas_admin?useSSL=false&useUnicode=true&characterEncoding=UTF-8";                 conn = (Connection) DriverManager.getConnection(url);                 System.out.println("成功连接数据库!!");             }             catch (SQLException ex1)             {                 System.out.print("取得连接的时候有错误,请核对用户名和密码");             }             return conn;         }       public static void main(String[]args)       {           GetConnection getConn=new GetConnection();           getConn.getConnection();       }}