maven工程运行环境修改
<plugins> <plugin> <!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin --> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8888</port> </configuration> </plugin> </plugins>
maven的java工程取mysql数据库数据
实体类 package com.example.day_24_maavenmysql.domain; public class Items { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
接口 package com.example.day_24_maavenmysql.dao; import com.example.day_24_maavenmysql.domain.Items; import java.util.List; public interface ItemsDao { public List<Items> findAll(); }
实现接口 package com.example.day_24_maavenmysql.dao.impl; import com.example.day_24_maavenmysql.dao.ItemsDao; import com.example.day_24_maavenmysql.domain.Items; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * 要想从数据库中取出数据 * 必须有四个属性:数据库驱动,连接数据库的地址,数据库用户名称,数据库密码。 */ public class ItemsDaoImpl implements ItemsDao { public List<Items> findAll(){ List<Items> list = new ArrayList<Items>(); //先获取contection对象 Connection connection = null; //获取真正操作数据的对象 PreparedStatement pst = null; //执行数据库查询操作 ResultSet rs = null; try { //加载驱动类 Class.forName("com.mysql.jdbc.Driver"); //先获取contection对象 connection = DriverManager.getConnection("jdbc:mysql:///kaohe","root", "root"); //获取真正操作数据的对象 pst = connection.prepareCall("select * from province"); //执行数据库查询操作 rs = pst.executeQuery(); //把数据库结果集转成java的List集合 while (rs.next()){ Items items = new Items(); items.setId(rs.getInt("id")); items.setName(rs.getString("name")); list.add(items); } }catch (Exception e){ e.printStackTrace(); } return list; } }
测试类 import com.example.day_24_maavenmysql.dao.ItemsDao; import com.example.day_24_maavenmysql.dao.impl.ItemsDaoImpl; import com.example.day_24_maavenmysql.domain.Items; import org.junit.Test; import java.util.List; public class ttest { @Test public void findAll(){ ItemsDao itemsDao = new ItemsDaoImpl(); List<Items> list = itemsDao.findAll(); for (Items items : list) { System.out.println(items.getName()); } } }
标签:24,java,day,maven,mysql,import,com,public From: https://www.cnblogs.com/yuzong/p/17113473.html