PDA

View Full Version : jdbc problem (using mysql)


inkedmn
10-25-2002, 03:38 AM
alright, i've been screwing around with this for about the past 3 hours now, and i'm about ready to set fire to this machine.

so please help me :)

ok, here's my code:


import java.sql.*;

public class Database {

private String host = "localhost";
private String user = "dbuser";
private String pass = "dbuser";
private String dbname = "joana";
private String url = "jdbc:mysql:joana";
protected Connection c;
protected Statement stmt;

public Database() {

Class.forName("org.gjt.mm.mysql.Driver");
c = DriverManager.getConnection(url, user, pass);
stmt = c.createStatement();
}

protected String insertFactoid(String key, String value, String sender) {
try {

stmt.executeUpdate("INSERT INTO FACTOID " +
"VALUES (" + key + " ," + value + " ." + sender + ") ");
return "ok";
} catch (Exception sqlex) {
return "there was a problem. tell inkedmn what you typed";
}
}

public static void main(String[] args) {
Database db = new Database();
String out = db.insertFactoid("test", "testing factoid db", "inkedmn");
System.out.println(out);
}
}

and here are my compiler errors...


D:\development\java_practice\myjava\joana>javac Database.java
Database.java:36: incompatible types
found : java.sql.Connection
required: Connection
c = DriverManager.getConnection(url, user, pass);
^
Database.java:37: cannot resolve symbol
symbol : method createStatement ()
location: class Connection
stmt = c.createStatement();
^
2 errors


i've followed the jdbc tutorial to the freakin' letter. all i know is, the docs say to use the createStatement() method, but it's not in the documentation under the Connection interface.

and the first error is a complete mystery to me...

HELP!

thanks :)

inkedmn
10-25-2002, 05:49 AM
all i can say is that TTT in #java on freenode r0x0rz :)

here's the working code (so far)

import java.sql.*;

public class Database {

private String host = "localhost";
private String user = "dbuser";
private String pass = "dbuser";
private String dbname = "joana";
private String url = "jdbc:mysql://localhost/joana";
protected java.sql.Connection c;
protected PreparedStatement ps;

public Database() {
try {
Database db = new Database();
Class.forName("org.gjt.mm.mysql.Driver");
c = DriverManager.getConnection(url, user, pass);
} catch (Exception e) {
System.out.println(e);
}
}
protected String insertFactoid(String key, String value, String sender)
throws java.sql.SQLException {
try {
ps = c.prepareStatement("insert into factoid values (?, ?, ?)");
ps.setString(1, key);
ps.setString(2, value);
ps.setString(3, sender);
ps.executeUpdate();
return "factoid created";
} catch (Exception sqlex) {
System.out.println(sqlex);
sqlex.printStackTrace();
return "there was a problem. tell inkedmn what you typed";
}
}

}


:)