eclipse上的maven建立

eclipse上的maven建立

  • maven
    • maven的安装
    • eclipse上maven配置
    • 创建maven项目
    • 报错
    • maven导入包
    • 与MySQL数据库连接

maven

maven的安装

  1. 将maven的压缩包解压(在train\maven里)出现bin,conf…,
  2. 将环境变量 新建m2_home,将地址放入,比如C:\maven\apache-maven-3.5.4;
  3. 将环境变量 path后面添加%m2_home%\bin;(发现要用全部才可以,替换不得行C:\maven\apache-maven-3.5.4\bin;)
  4. 在命令行中用mvn -v检验
    eclipse上的maven建立_第1张图片

eclipse上maven配置

maven的包的位置:
window->preferences->Maven->

  1. installations :add ,文件位置 ,apply
  2. User Settings找到。 User Settings。修改C:\maven\apache-maven-3.5.4\conf\settings.xml。
    一般是在:{user.home}.m2\repository

错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quick
下载quick
解压把maven-archetype-quickstart-1.1.jar 放到maven安装目录对应的lib包下。
然后打开dos窗口切换到lib包下(shift+右键:在此处打开),输入以下命令即可:mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-quickstart -Dversion=1.1 -Dpackaging=jar -Dfile=maven-archetype-quickstart-1.1.jar (标题栏,右键,快速编辑模式,右键则粘贴,右键+enter 复制)

创建maven项目

链接

报错

Eclipse创建maven项目时报错:maven-resources-plugin:2.5 or one of its dependencies could not be resolved
解决:修改settings.xml,添加:

 
        Central 
        http://repo1.maven.org/maven2 
        central 
 

而后删除项目重新创建,或者Force Update

maven导入包

链接

与MySQL数据库连接

查看数据库名称 :查询中show databases
eclipse上的maven建立_第2张图片

// An highlighted block
package exam_pro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Exam {
	public static void main(String[] args) {
		//声明Connection对象
		Connection con;
		//驱动程序名
		String driver = "com.mysql.jdbc.Driver";
		//URL指向要访问的数据库名mydata
		String url = "jdbc:mysql://localhost:3306/mysql";
		//MySQL配置时的用户名
		String user = "root";
		//MySQL配置时的密码
		String password = "beanbean";
		//遍历查询结果集
		try {
		//加载驱动程序
			Class.forName(driver);
			//1.getConnection()方法,连接MySQL数据库!!
			con = DriverManager.getConnection(url,user,password);
			if(!con.isClosed())
				System.out.println("Succeeded connecting to the Database!");
			//2.创建statement类对象,用来执行SQL语句!!
			Statement statement = con.createStatement();
			 //要执行的SQL语句
			String sql = "select*from admin;";
			//3.ResultSet类,用来存放获取的结果集!!
			ResultSet rs = statement.executeQuery(sql);
			System.out.println("-----------------");
			System.out.println("执行结果如下所示:");  
			System.out.println("-----------------");  
			System.out.println("姓名" + "\t" + "电话");  
			System.out.println("-----------------");  
			
			String id = null;
			String name = null;
			String tele = null;
			while(rs.next()){
				//获取stuname这列数据
				id = rs.getString("id");
				//获取stuid这列数据
				name = rs.getString("name");
				tele=rs.getString("tele");
                //输出结果
				System.out.println(name + "\t" + tele);
             }
             rs.close();
             con.close();
         } catch(ClassNotFoundException e) {   
             //数据库驱动类异常处理
             System.out.println("Sorry,can`t find the Driver!");   
             e.printStackTrace();   
             } catch(SQLException e) {
              //数据库连接失败异常处理
             e.printStackTrace();  
             }catch (Exception e) {
             // TODO: handle exception
             e.printStackTrace();
         }finally{
             System.out.println("数据成功获取!!");
         }
     }

}

你可能感兴趣的:(eclipse上的maven建立)