赞
踩
首先右击此电脑点击管理,进入页面
再服务栏确保MySQL是正常运行状态
打开IDEA,
左边栏选择Maven Archetype,新建一个名为javaweb的新工程
进行如图编辑完成新建
在Main包下新建一个java包,右击java包进行下图操作,java包拥有新建class的权限
在java下面新建一个HelloConnection1的类,用浏览器输入https://mvnrepository.com/进入Maven网页,输入mysql
进入 MySQL Connector Java,选择5.1.40版本进入
复制如下文字到IDEA中pom.xml中
在HelloConnection1中输入如下编码
import java.sql.Connection; import java.sql.DriverManager; public class HelloConnection1 { public static void main(String[] args) { try { //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 //jdbc:mysql://localhost:3306/test // 技术名字 :连接的数据库://ip:端口/数据库名 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","abc123"); System.out.println(connection); } catch (Exception e) { e.printStackTrace(); } } }
运行结果如上图即为连接成功
新建HelloConnection2的类,再新建一个file格式的文件,名为jdbc.properties,进入里面进行编辑输入如下代码
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.user=root jdbc.poassworld=abc123
再进入类中输入代码
import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; public class HelloConnection2 { public static void main(String[] args) { Properties pro=new Properties(); try { InputStream resourceAsStream = HelloConnection2.class.getClassLoader().getResourceAsStream("jdbc.properties"); pro.load(resourceAsStream); String className = pro.getProperty("jdbc.driverClass"); String url = pro.getProperty("jdbc.url"); String user=pro.getProperty("jdbc.user"); String password = pro.getProperty("jdbc.poassworld"); Class.forName(className); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); } catch (Exception e) { e.printStackTrace(); } } }
运行结果如上图所示即为连接成功
输入如下代码到pom.xml文件中,新建HelloConnection3的类,输入如下代码
import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; public class HelloConnection3 { public static void main(String[] args) { ComboPooledDataSource cpds = new ComboPooledDataSource(); try { cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" ); cpds.setUser("root"); cpds.setPassword("abc123"); Connection connection = cpds.getConnection(); System.out.println(connection); } catch (Exception e) { e.printStackTrace(); } } }
运行结果如图所示即为连接成功
c3p0-config> <!-- This app is massive! --> <named-config name="hello"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> <property name="user">root</property> <property name="password">abc123</property> </named-config> </c3p0-config>
新建类,HelloConnection4,输入如下代码
import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.SQLException; public class HelloConnection4 { public static void main(String[] args) { ComboPooledDataSource cpds= new ComboPooledDataSource("hello"); try { Connection connection = cpds.getConnection(); System.out.println(connection); } catch (SQLException e) { e.printStackTrace(); } } }
运行结果如图所示即为连接成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。