使用java查询MySQL数据库 [英] Query a MySQL db using java

查看:718
本文介绍了使用java查询MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,简单地说,我有一个带有文本输出框的java应用程序。我想查询一个Db并将输出显示到一个文本框中。

Guys, simply put, I have a java application with a text output box. I'd like to query a Db and display the output into a text box.

示例我有一个Db,两列 food color

Example I have a Db with two columns food and color

我想:

SELECT * in Table WHERE color = 'blue'

有任何建议吗?

推荐答案

初学者通常面临的问题,了解如何从Java连接到MySQL。这是可以帮助您快速启动和运行的代码段。你必须从某处获取mysql jdbc驱动程序jar文件(google it),并将其添加到类路径。

Beginners generally face problems understanding how to connect to MySQL from Java. This is the code snippet that can get you up and running quickly. You have to get the mysql jdbc driver jar file from somewhere (google it) and add it to the classpath.

Class.forName("com.mysql.jdbc.Driver") ;
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "usrname", "pswd") ;
Statement stmt = conn.createStatement() ;
String query = "select columnname from tablename ;" ;
ResultSet rs = stmt.executeQuery(query) ;

这篇关于使用java查询MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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