Scardaddy
Member
+37|6863|UK
OK guys I will keep it short and simple.

You guys have really helped me in the past and I wanted to say thanks for that, but I have a question about java and linking it to a database.  I hope someone hear can help me.  I have looked in various places, but couldn't get any help, so I'm asking you guys

I have written an applet in java and wanted some of the text fields to access a MySQL database, but I'm at a total loss cause I can not find any reference to any source code at all on this subject.  If anyone out there knows how this works and could help me I would greatly appreciate it.


Thanks guys.
Vilham
Say wat!?
+580|7237|UK
If i was at uni i could link you my coursework that did this but im not, however i think i can find the code for it.

Our Database is MySQL This is brilliant and free!
Driver is mysql-connect.jar
java -cp .\mysql-connect.jar
Or put it in:
    C:\j2sdk1.x.x\jre\lib\ext
This is always in the CLASSPATH

About the Database
host = “sauron.cs.nott.ac.uk”;
database = “wisdom”;
port  = 3306;
user = “xxx03u”;
password = “ISentYouThis";


Register the Driver
Connect to Database
Create and Execute a Statement
Statement st = con.createStatement();
String qry = “SELECT ...”;
ResultSet rs = st.executeQuery(qry);

String qry = “INSERT ...”;
st.executeUpdate(qry);
JDBC Statements

Getting the data returned
String code = rs.getString(“code");
Needs to be in a try block
Of course other methods are:

getInt();
getDouble();
Navigating the ResultSet
while(rs.next()) {
    … process a row
}


How to connect in Java
DBconnect() {
       try  {
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             pConnection=DriverManager.getConnection("jdbc:odbc:mydb.mdb","","");
             pStatement = pConnection.createStatement();
       }
       catch(Exception e) {
             e.getMessage(); // print this out
       }
}



Those are the bullet points from the slides. I think the last section is the bit u need.

Last edited by Vilham (2007-05-16 06:18:16)

Slayer
---hates you
+1,137|7227|Hell, p.o box 666

moved to tech!!! Check the subsection next time, thanks!
Scardaddy
Member
+37|6863|UK
Thanks for your help dude. I got it done but abandoned the java as it was apparently a bad way to connect to a database through an applet, oh well thanks again.

Board footer

Privacy Policy - © 2025 Jeff Minard