Java and mysql??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Java and mysql??

Post by Jade »

Hey there,

I finally took the plunge into Java and I have no clue how to query mysql from an applet. Anyone out there know what I need to do?? Thanks for any help you can give me.

Jade
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You must download the MySQL JDBC Driver. It can be downloaded here[MySQL Connector/J 3.0]. Open the instructions file (README) and follow the installation steps.

Then go to the JDBC API Documentation and follow the JDBC Basics lesson.

I took a couple hours to install things and perform various tests. One of them (pretty simple):

Code: Select all

import java.sql.*;

/**
 *
 * @author  Scorphus
 */
public class JDBCTest {
	
	/** Creates a new instance of JDBCTest */
	public JDBCTest() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bonsuc?user=bonsuc&password=bonsuc");
			Statement stmt = conn.createStatement();
			String query = "show tables";
			ResultSet rs = stmt.executeQuery(query);
			while (rs.next()) {
				String s = rs.getString(1);
				System.out.println(s);
			}
			rs.close();
		}
		catch (Exception e) {
			System.out.println("Error: " + e);
		}
		return;
	}

	public static void main (Stringї] args) {
		JDBCTest test = new JDBCTest();
		return;
	}

}
Hope it helps. Regards,
Scorphus.
Post Reply