Page 1 of 1

Java and mysql??

Posted: Fri Oct 31, 2003 8:08 am
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

Posted: Fri Oct 31, 2003 8:30 am
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.