Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query a MySQL db using java

Tags:

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.

Example I have a Db with two columns food and color

I'd like to :

SELECT * in Table WHERE color = 'blue' 

Any suggestions?

like image 988
Lexicon Avatar asked Apr 27 '11 19:04

Lexicon


People also ask

How do you query a database in Java?

STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.

Can Java connect to MySQL database?

Accessing the MySQL Database with Java. MySQL database schemas can be accessed from applications written in Java. MySQL Connector/J is a JDBC driver that allows Java applications to connect to a MySQL database. This Note gives an example of using MySQL Connector/J to connect to MySQL databases in the School.

What is JDBC URL for MySQL?

jdbc. Driver. Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and sonoo is the database name.


2 Answers

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) ; 
like image 102
euphoria83 Avatar answered Sep 20 '22 14:09

euphoria83


You should use JDBC. See http://en.wikipedia.org/wiki/Java_Database_Connectivity

You need the Java MySQL Connector from http://dev.mysql.com/downloads/connector/j/

Then use something like (copied from the Wikipedia article):

Class.forName( "com.mysql.jdbc.driver" );  Connection conn = DriverManager.getConnection(  "jdbc:mysql://localhost/database",  "myLogin",  "myPassword" ); try {      Statement stmt = conn.createStatement(); try {     ResultSet rs = stmt.executeQuery( "SELECT * FROM Table WHERE color = 'blue'" );     try {         while ( rs.next() ) {             int numColumns = rs.getMetaData().getColumnCount();             for ( int i = 1 ; i <= numColumns ; i++ ) {                // Column numbers start at 1.                // Also there are many methods on the result set to return                //  the column as a particular type. Refer to the Sun documentation                //  for the list of valid conversions.                System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );             }         }     } finally {         try { rs.close(); } catch (Throwable ignore) { /* Propagate the original exception instead of this one that you may want just logged */ }     } } finally {     try { stmt.close(); } catch (Throwable ignore) { /* Propagate the original exception instead of this one that you may want just logged */ } } } finally {     //It's important to close the connection when you are done with it     try { conn.close(); } catch (Throwable ignore) { /* Propagate the original exception instead of this one that you may want just logged */ } } 
like image 20
ert Avatar answered Sep 21 '22 14:09

ert