maven工程运行环境修改与maven的java工程取mysql数据库数据
maven工程运行环境修改
在pom.xml添加tomcat7插件
</plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin>
这样的话整个模板被调用出来了
maven的java工程取mysql数据库数据
将mysql数据库的jar导入到pom.xml
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.32</version> </dependency>
package nj.xueqiang.dao.impl; import nj.xueqiang.dao.UserDao; import nj.xueqiang.domain.User; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * 要想从数据库中取出数据 * 必须有四个属性:数据库驱动,连接数据库地址,数据库用户名名称,数据库密码 */ public class UserDaoimpl implements UserDao { @Override public List<User> findAll() throws SQLException, ClassNotFoundException { ArrayList<User> users = new ArrayList<>(); //先获取contenction对象 Connection connection = null; //获取真正操作数据库的对象 PreparedStatement call = null; //执行数据库查询操作 ResultSet query =null; try { //加载驱动类 Class.forName("com.mysql.cj.jdbc.Driver"); //先获取contenction对象 connection = DriverManager.getConnection("jdbc:mysql:///day17","root","njzyb555"); //获取真正操作数据库的对象 call = connection.prepareCall("select * from user"); //执行数据库查询操作 query = call.executeQuery(); //把数据库结果集转成java的List集合 while (query.next()){ User user = new User(); user.setId(query.getInt("id")); user.setName(query.getString("name")); user.setGender(query.getString("gender")); user.setAge(query.getInt("age")); user.setAddress(query.getString("address")); user.setQq(query.getString("qq")); user.setEmail(query.getString("email")); user.setUsername(query.getString("username")); user.setPassword(query.getString("password")); users.add(user); } }catch (Exception e){ e.printStackTrace(); }finally { connection.close(); call.close(); query.close(); } return users; } }
package nj.xueqiang.dao; import nj.xueqiang.domain.User; import java.sql.SQLException; import java.util.List; public interface UserDao { public List<User> findAll() throws SQLException, ClassNotFoundException; }
package nj.xueqiang.domain; public class User { private Integer id; private String name; private String gender; private int age; private String address; private String qq; private String email; private String username; private String password; 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; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package hs.xueqiang.test; import nj.xueqiang.dao.UserDao; import nj.xueqiang.dao.impl.UserDaoimpl; import nj.xueqiang.domain.User; import org.junit.Test; import java.sql.SQLException; import java.util.List; public class UserTest { @Test public void findAll() throws SQLException, ClassNotFoundException { UserDao userDao = new UserDaoimpl(); List<User> all = userDao.findAll(); for (User user:all){ System.out.println(user.getName()); } } }