douya2433 2016-05-30 16:59
浏览 160
已采纳

使用Java和JSP连接到Web数据库

I'm trying to learn java and how to use it for web applications . I'm already familiar with PHP so i'm trying to learn how to use JSP since it seems to be the closest thing to PHP in Java. My school provides us with a fre MySQL v5.1 databases so I'm using that as my db. When connecting to a database in PHP I use mysqli and do

$mysqli = new mysqli("oniddb.cws.oregonstate.edu", "username, "pword", "username");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} 

For Java and JDBC I'm having a hard time finding the equivalent to this. Everything I've found implies my database is local and not over the web. How can I do this in JSP?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doulingqiu4349 2016-05-30 17:13
    关注

    Сonnecting to a database directly from JSP is possible, but it is a bad practice.

    JSP is designed to be a view, but not a place where "back-end" logic happens. Ideally, it should just use parameters that are passed from a backend.

    If I were you I would start with learning JDBC (API for connecting to databases from Java). Here is a tutorial from Oracle

    And after learning JDBC you can apply you knowledge in web using MVC pattern for example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器