XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
Deemo
Forum Contributor
Posts: 418 Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC
Post
by Deemo » Mon Nov 29, 2004 3:34 pm
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
Thanks alot
Deemo
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Mon Nov 29, 2004 4:05 pm
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.
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Sun Dec 05, 2004 12:42 pm
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