当前位置: 代码网 > it编程>编程语言>Java > maven项目连接MySQL方式(使用原生的jdbc)

maven项目连接MySQL方式(使用原生的jdbc)

2026年03月12日 Java 我要评论
1.引入依赖<dependency><groupid>mysql</groupid><artifactid>mysql-connector-java&l

1.引入依赖

<dependency>

<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.45</version>

</dependency>

2.项目目录结构

jdbc.properties内容

driver=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/shiro?useunicode=true&characterencoding=utf-8&usessl=false

username=root

password=123456

util类

package com.cxb.shiro;


import java.io.ioexception;
import java.util.properties;


/**
 * 获取资源文件的util
 * @author 81046
 *
 */
public class propertiesutil {
static properties properties = new properties();


    public propertiesutil() {
    }
    public static boolean loadfile(string filename){
        try {
            properties.load(propertiesutil.class.getclassloader().getresourceasstream(filename));
        } catch (ioexception e) {
            e.printstacktrace();
            return false;
        }
        return true;
    }
    public static string getpropertyvalue(string key){
        return properties.getproperty(key);
    }


}
package com.cxb.shiro;

import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;


public class jdbcutils {


//通过上面的工具就可以获取到properties文件中的键值从而可以加载驱动 获取链接 从而 可以增删改查
    private static connection conn = null;




    public static connection getconn(){
        propertiesutil.loadfile("jdbc.properties");
        string driver = propertiesutil.getpropertyvalue("driver");
        string url = propertiesutil.getpropertyvalue("url");
        string username  = propertiesutil.getpropertyvalue("username");
        string password = propertiesutil.getpropertyvalue("password");


        try {
            class.forname(driver);
            conn = drivermanager.getconnection(url,username,password);


        } catch (classnotfoundexception e) {
            e.printstacktrace();
        } catch (sqlexception e) {
            e.printstacktrace();
            close();
        }
        return conn;
    }
    
    
    public static void close(){
        try {
            conn.close();
        } catch (sqlexception e) {
            e.printstacktrace();
        }
    }

}



/**
* 通过用户名到数据库中获取凭证密码
* @param username
* @return
*/
private static string getpasswordbyusername(string username) {
//sql语句  
string sql = "select password from users where username = " +"'" + username+"'";
    connection conn = jdbcutils.getconn();
    statement stmt=null; 
    resultset ret = null;  
    string password=null;
try {
stmt = conn.createstatement();
//执行语句,得到结果集  
ret = stmt.executequery(sql);
            while (ret.next()) {  
            //这里只查询的密码
            password = ret.getstring(1);  
            }
            ret.close();  
            conn.close();//关闭连接  
} catch (sqlexception e1) {
e1.printstacktrace();
}  
return password;
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com