首页 > 数据库技术 > 详细

本地 MySQL 数据库和本地服务关联

时间:2019-11-04 12:30:09      阅读:98      评论:0      收藏:0      [点我收藏+]

本地 MySQL 数据库和本地服务关联

(一)创建本地数据库

???? (,,?? . ??,,) 大爷,来看看吧

(二)创建本地服务

???? (,,?? . ??,,) 真的不要钱

(三)将服务和数据库关联????划重点,会考试哦

????我这里用的是IntelliJ IDEA,其他工具我没有了解,想来应该差不多,这里配置账号密码,数据库的连接地址。
????吧啦吧啦,没有代码和图的解释,都是耍流氓。

# -------- 数据库配置  --------
#mysql驱动包名
driver=com.mysql.cj.jdbc.Driver
#数据库连接地址
url=jdbc:mysql://localhost:3306/lemon
#用户名
user=root
#密码
password=***
server.port=8081

技术分享图片

(四)上码测试啦

@Repository
public class UserDao {
    public String getUserListDao (UserInfo userInfo) {
        String text = "";
        Connection connection = null;
        try {
            //获取数据库连接
            connection = MySQLConn.getConnection();
            //mysql查询语句
            String sql = "SELECT * FROM authors";
            PreparedStatement prst = connection.prepareStatement(sql);
            //结果集
            ResultSet rst = prst.executeQuery();
            while (rst.next()) {
                System.out.println("用户名:" + rst.getString("username"));
                text = text + "【用户名:" + rst.getString("username") + "】";
            }
            MySQLConn.closeResultSet(prst, rst);
        } catch (Exception e) {
            System.out.println("catch");
            System.out.println(e);
            e.printStackTrace();
        }finally {
            MySQLConn.closeConn(connection);
        }
        return text;
    }
}

????MySQLConn是我自己创建的一个专门连接数据库的类,方便自己的调用。

????贴出来不要耍流氓 (???)

????Tools 是个打印的工具类,不赘述了,可以用 System.out.println("");

public class MySQLConn {

    //mysql驱动包名
    private static String DRIVER;
    //数据库连接地址
    private static String URL;
    //用户名
    private static String USER;
    //密码
    private static String PASSWORD;

    static {
        try {
            ClassLoader classLoader = MySQLConn.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream("application.properties");
            Properties pt = new Properties();
            pt.load(is);
            DRIVER = pt.getProperty("driver");
            URL = pt.getProperty("url");
            USER = pt.getProperty("user");
            PASSWORD = pt.getProperty("password");
        }catch (IOException e){
            Tools.Log("获取配置文件报错 " + e);
            e.printStackTrace();
        }
    }


    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(DRIVER);
            conn= DriverManager.getConnection(URL, USER, PASSWORD);
        }catch (Exception e){
            Tools.Log("Connection 连接报错" + e);
            e.printStackTrace();
        }
        return conn;
    }

    //关闭查询和结果集
    public static void closeResultSet(PreparedStatement pstmt, ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            }catch (SQLException e){
                Tools.Log("ResultSet 关闭报错" + e);
                e.printStackTrace();
            }
        }
        if (pstmt!=null){
            try {
                pstmt.close();
            }catch (SQLException e){
                Tools.Log("PreparedStatement 关闭报错" + e);
                e.printStackTrace();
            }
        }
    }

    //关闭连接
    public static void closeConn(Connection conn) {
        if (conn!=null){
            try {
                conn.close();
            }catch (SQLException e){
                Tools.Log("Connection 关闭报错" + e);
                e.printStackTrace();
            }
        }
    }


}

????为了方便测试,我随便创建了几条数据,测试输出数据库的内容,就表示我们的本地数据库和服务项目已经成功连接。

技术分享图片

技术分享图片

技术分享图片

本地 MySQL 数据库和本地服务关联

原文:https://www.cnblogs.com/OrgCheng/p/11791217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!