Java开发环境创建maven,jdbc连接数据库
程序员文章站
2022-06-28 17:12:33
配置maven:我们最好是将E:\apache-maven-3.5.4\apache-maven-3.5.4\conf里面的settings文件复制一份然后自己创建一个文件夹单独放在里面:我们打开settings进行配置:1.本地仓库配置localRepository:2、配置阿里镜像服务器mirrors标签内部: alimaven aliyun maven
配置maven:
我们最好是将E:\apache-maven-3.5.4\apache-maven-3.5.4\conf里面的settings文件复制一份然后自己创建一个文件夹单独放在里面:
我们打开settings进行配置:
1.本地仓库配置localRepository:
2、配置阿里镜像服务器mirrors标签内部:
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
创建maven:
设置:
如果你发现自己默认选择的不是你创建的那个本地仓库的文件夹,你把override打钩,然后自己选择(一般这里会根据你的settings配置的本地仓库路径来自动给你配置好,一般不会出错):
创建:
这样就创建成功了!
jdbc连接数据库:
配置到pom.xml文件中:
我们一般普通的连接方式:
//用户名
String user = "root"; //密码
String password = "123456"; //url
String url = "jdbc:mysql://localhost:3306/db2?serverTimezone=GMT%2B8"; //注册驱动
Class.forName("com.mysql.cj.jdbc.Driver"); //建立连接
Connection connection = DriverManager.getConnection(url, user, password); //sql语句
String sql = "select * f" + "rom t_mysql_beauty"; //预处理sql语句
PreparedStatement preparedStatement = connection.prepareStatement(sql); //执行sql语句
ResultSet resultSet = preparedStatement.executeQuery(); //处理sql语句执行的结果 while (resultSet.next()) { System.out.println(resultSet.getInt("id")); System.out.println(resultSet.getString("name")); } //释放资源
connection.close(); preparedStatement.close();
通过上面这个连接方式我们可以看到这代码是不灵活的,如果有其他的需求有很多的代码的都是重复的,所以我们要进行优化:
- 1、针对于不同用户,jdbc对应的URL不同,用户名密码不同,其他是一样的;
- 2、针对于不同的需求,变动的部分是SQL语句,结果的处理不同,其他又是一样的;
- 3、MySQL版本不一样,驱动包的类的全路径名还不一样;**
将用户名密码跟url写入一个file文件里面然后,如果需要更改直接读文件即可:
mysql.Driver = com.mysql.cj.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/db2?serverTimezone=GMT%2B8
mysql.user = root
mysql.password = 123456
自定义工具类:
public class DBAccess { // 加载驱动
private static String driver; private static String url; private static String userName; private static String password; static { InputStream in = DBAccess.class.getResourceAsStream("/jdbc.properties"); Properties p = new Properties(); try { p.load(in); driver = p.getProperty("mysql.Driver"); url = p.getProperty("mysql.url"); userName = p.getProperty("mysql.userName"); password = p.getProperty("mysql.password"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,userName,password); } public static void close(ResultSet rs,PreparedStatement pst,Connection con) throws SQLException { close(rs); close(pst); close(con); } private static void close(Connection con) throws SQLException { if(con != null) { con.close(); } } private static void close(PreparedStatement pst) throws SQLException { if(pst != null) { pst.close(); } } public static void close(ResultSet rs) throws SQLException { if(rs != null) { rs.close(); } } }
public class BaseDao<T> { public List<T> executeQuery(String sql, Class clz) throws Exception { List<T> list = new ArrayList<T>(); // 2、建立链接
Connection con = DBAccess.getConnection(); // 3、预处理sql语句
// 查询出最近写的10篇博客的标题及摘要,展示到控制台
// 最新使用博客系统的用户
PreparedStatement pst = con.prepareStatement(sql); // 4、执行sql语句
ResultSet rs = pst.executeQuery(); T t; // 5、处理sql语句执行的结果 while (rs.next()) { // list.add(new Blog(rs.getInt("bid"), rs.getString("title"), rs.getString("summary"))); /*
* 1、实例化了一个T对象
* 2、给这个泛型对象所有属性赋值了
* 3、将t加入到集合中
*/
t = (T) clz.newInstance(); Field[] fields = clz.getDeclaredFields(); for (Field f : fields) { f.setAccessible(true); f.set(t, rs.getObject(f.getName())); } list.add(t); } // 6、释放资源(链接、结果集、预定义对象)
DBAccess.close(rs, pst, con); return list; } }
定义一个博客类:
public class Blog { private int bid; private String title; private String summary; public Blog(int bid, String title, String summary) { super(); this.bid = bid; this.title = title; this.summary = summary; } public Blog() { super(); } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } @Override
public String toString() { return "Blog [bid=" + bid + ", title=" + title + ", summary=" + summary + "]"; } }
定义一个用户类:
public class User { private int uid; private String userName; private String realName; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } @Override
public String toString() { return "User [uid=" + uid + ", userName=" + userName + ", realName=" + realName + "]"; } public User(int uid, String userName, String realName) { super(); this.uid = uid; this.userName = userName; this.realName = realName; } public User() { super(); } }
测试:
public class BlogDao extends BaseDao<Blog> { public List<Blog> list() throws Exception { //根据自己查询的类容,修改类.class; return super.executeQuery("select * from t_p1_blog limit 10", Blog.class); } public static void main(String[] args) throws Exception { BlogDao blogDao = new BlogDao(); //返回最新查看博客的用户
List<Blog> list = blogDao.list(); for (Blog b : list) { System.out.println(b); } } }
通过代码以上的优化,我们可以看到,sql语句,连接的用户,与查询的代码都不在会是死的;
本文地址:https://blog.csdn.net/LetMeKangKang/article/details/107892634