Page 1 of 1

Java MySQL Class

Posted: Mon Nov 29, 2004 3:34 pm
by Deemo
Hey everyone

im working on a personal MySQL object that is modelled after the PHP functions. now i got some of the actual thinking up part of it done, but the only thing i cant figure out is how to connect to a MySQL server and send queries and stuff back and forth using Java. Ive never really used MySQL at the command line (only a bit when i was first learning it) and i would greatly appreciate any help. This is the only difficulty i am having right now and if i can figure this out it would help me alot :D

Thanks alot
Deemo

Posted: Mon Nov 29, 2004 4:05 pm
by kettle_drum
I would guess that you create a socket to the server and then follow the protocol that it uses. Check to see how other java mysql classes do it.

Posted: Sun Dec 05, 2004 12:42 pm
by ol4pr0
Enjoy

go to http://dev.mysql.com/go/Downloads/ and download this driver u will need it

Code: Select all

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

public class mysql_connect
{
private static String contactpersoon;

public static void main(Stringї] args) {
try
{

	Class.forName("com.mysql.jdbc.Driver").newInstance();
	//Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex) {
// handle the error
}

try {
Connection conn = DriverManager.getConnection("jdbc:mysql://host/DATABASE", "USER", "PASS");//this is your db name(new)
System.out.println("Connected to database");
ResultSet rst=null;
Statement stmt=null;
stmt=conn.createStatement();
rst = stmt.executeQuery(
        "SELECT id,whatever u need more FROM table " + 
        "ORDER BY id LIMIT ?" );

      // retrieve results
      while (rst.next()) {
      	int id = rst.getInt("key_id");
        String Astring1 = rst.getString("row_from_select_statement");
        String Astring2 = rst.getString("row_from_select_statement");
        String Astring3 = rst.getString("row_from_select_statement");
       	System.out.println("ID: " + id + "  Astring1: " + Astring1 + "Astring2: " + Astring2 + "Astring3: " + Astring3);
        	
        }
      }
  }
catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}

}
}

This was just for the select however update() works almost the same

calling a java class you could do like this

Code: Select all

$send = new Java('javaClass', "{whatever parameteres}");
$answer = $send->funcion_to_call_to_get_what_you_need();
PS. you could of found lots of example loooking like this on sun if you really wanted to :)