无法连接到MySQL并在Play Framework 2.4中执行操作 [英] Unable to connect to MySQL and perform operations in Play Framework 2.4

查看:98
本文介绍了无法连接到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.

  1. 我还没有模特.
  2. 我将来可能会要求,但现在不是.
  3. 当前我只想访问现有表.

作为一个旁注,我能够使用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,我建议

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中执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆