首页 > 编程知识 正文

idea如何配置数据库连接,数据库怎么连接idea

时间:2023-05-06 02:49:25 阅读:240203 作者:4844

连接数据库的五种方式

方式一

public void testConnection() throws SQLException { Driver driver=new com.mysql.cj.jdbc.Driver(); //tset指数据库名,根据名字不同改,3306为端口号 String url="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; //将用户名和密码封装 Properties into=new Properties(); //用户名和密码需根据你所设置的填写 into.setProperty("user","用户名"); into.setProperty("password","密码"); Connection connect = driver.connect(url, into); System.out.println(connect); }

方式二

public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { //获取Driver实例化对象,使用反射 Class aClass = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver= (Driver) aClass.newInstance(); //提供连接数据库 String url="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; //提供用户名,密码 Properties into=new Properties(); into.setProperty("user","用户名"); into.setProperty("password","密码"); //获取连接 Connection connect = driver.connect(url, into); System.out.println(connect); }

方式三

public void test3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException { Class aClass = Class.forName("com.mysql.cj.jdbc.Driver"); Driver driver= (Driver) aClass.newInstance(); //基本信息 String url="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; String user="用户名"; String password="密码"; //注册驱动 DriverManager.registerDriver(driver); //获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }

方式四

public void test4() throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException { //基本信息 String url="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; String user="用户名"; String password="密码"; //加载Driver驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }

方式五(推荐使用这种方式连接数据库)

user=数据库用户名password=数据库密码url=jdbc:mysql://localhost:3306/数据库表名?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTCdriverClass=com.mysql.cj.jdbc.Driver public void test5() throws IOException, ClassNotFoundException, SQLException { //读取配置文件基本信息,jdbc。properties为文件名,根据不同命名修改 InputStream resourceAsStream = DriverConnect.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(resourceAsStream); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String url= properties.getProperty("url"); String driverClass= properties.getProperty("driverClass"); //加载驱动 Class.forName(driverClass); //获取连接 Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }

认可的小伙伴给个点赞哦,继续努力呀~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。