无法连接到MySQL并在Play Framework 2.4中执行操作

编程入门 行业动态 更新时间:2024-10-27 10:31:16
本文介绍了无法连接到MySQL并在Play Framework 2.4中执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对Play和Java非常陌生(我来自Python和Django背景:)).我正在尝试连接到MYSQL数据库并从中读取值.但是,尝试了几个令人沮丧的时间后,我无法这样做.请帮忙.

I am very new to Play and Java (i come from a Python and Django background :) ). I am trying to connect to MYSQL database and read values from it. However, after trying for about few frustrating hours i am unable to do so. Please Help.

我的build.sbt添加了依赖项:

My build.sbt has the dependency added:

"mysql" % "mysql-connector-java" % "5.1.18"

我的Application.conf文件为(相关部分)

My Application.conf file is as (the relevant section)

db.default.driver=com.mysql.jdbc.Driver db.default.url="jdbc:mysql://127.0.0.1:3306/myDBName" db.default.user="root" db.default.password="myPassword"

我正在尝试在应用程序启动时从Db读取值.稍后,我也想从控制器中的任何操作中进行操作.我的代码的相关部分:

And i am trying to read the values from Db on application start. Later i would also want to do it from any action in the controller. The relevant section of my code:

import java.sql.ResultSet; import java.sql.SQLException; import play.db.*; import javax.sql.DataSource; @Override public void onStart(play.Application appConfig){ DataSource dataSources = DB.getDataSource(); java.sql.Connection connectionSQL = DB.getConnection(); ResultSet rs = connectionSQL.prepareStatement("select * from mytable").executeQuery(); while(rs.next()) { System.out.println(rs.getString(1)); Logger.debug(rs.getString(1)); } }

但是我得到一个空指针异常.我还尝试了许多其他的SO问题,例如 Simple CRUD教程关于使用Ebean的Play框架和MySQL?,但它们都是以前的版本.同样,在这方面似乎缺少官方的文档.我知道这是一个相对较新的开源项目,但这只是我的两分钱.如果我能够做到这一点,我将尝试为像我这样迷失的灵魂发布一份官方教程.到那时,请帮助我. 谢谢

However i am getting a null pointer exception. I also tried many other SO questions such as Simple CRUD tutorial about Play Framework and MySQL using Ebean? but they are all for previous versions. Also,the official Documentation seems lacking in this regard. I understand it being a relatively new and open source project but that's just my two cents. If i am able to do this, i would try to publish an official tutorial for lost souls like me. Till then, please help me. Thanks

EDIT1 @biesior对不起,您丢失了这些详细信息....已在问题中添加了此内容.还.

EDIT1 @biesior Sorry for missing out these details....Have added in the question now. Also.

  • 我还没有模特.
  • 我将来可能会要求,但现在不是.
  • 当前我只想访问现有表.
  • 作为一个旁注,我能够使用Morphia对象创建模型并访问MongoDB并执行操作.所以我有一个基本的想法 ,但是到目前为止,我只需要使用play java访问现有的MySQL数据库

    Just as a side note i was able to create models and access MongoDB using Morphia object and perform operations. So i have the basic idea but as of now i just need to access existing MySQL db with play java

    推荐答案

    很好地使用Java,我建议 Ebean的SqlQuery API 用于执行此操作(也许是因为我更喜欢它;)).

    Well with Java I'd recommend Ebean's SqlQuery API for doing this (maybe because I just prefer it ;)).

    在 project/plugins.sbt 中取消注释(最后一个):

    In project/plugins.sbt uncomment the line (last one):

    addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")

    在 built.sbt 修改行并将 PlayEbean 添加到已启用的插件中,例如:

    In built.sbt modify line and add the PlayEbean to enabled plugins, like:

    lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

    在您的 conf/application.conf 中,在数据库配置之后添加以下行:

    In your conf/application.conf add this line after DB configs:

    ebean.default = ["models.*"]

    因此您可以使用它,即在您的操作中用作(样本ofc):

    So you can use it i.e. in your action as (sample ofc):

    SqlQuery query = Ebean.createSqlQuery("SELECT * FROM users WHERE username LIKE :username"); query.setParameter("username", "%200ok%"); List<SqlRow> rows = query.findList(); for (SqlRow row : rows) { play.Logger.debug("Found user: " + row.getString("username") + " with ID: " + row.getInteger("id")); }

    更多推荐

    无法连接到MySQL并在Play Framework 2.4中执行操作

    本文发布于:2023-07-13 03:21:21,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1099789.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:并在   连接到   操作   MySQL   Play

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!