help with inserting text into a database

hello

i have this code but i want to be able to insert into the database could someone please adjust my code so it works please

does it matter what i insert into the database?

import java.sql.*;

public class CoalTotals {

public static void main(String[] args) {

String data = "jdbc:odbc:myProject";

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn = DriverManager.getConnection(

data, "", "");

Statement st = conn.createStatement();

ResultSet rec = st.executeQuery(

"SELECT * " +

"FROM Coal " +

"WHERE " +

"(Country='" + args[0] + "') " +

"Order by Year");

System.out.println("Fips\tCountry\tYear\t" +

"Anthracite production");

while(rec.next()) {

System.out.println(rec.getString(1) + "\t"

+ rec.getString(2) + "\t"

+ rec.getString(3) + "\t"

+ rec.getString(4));

}

st.close();

} catch (SQLException s) {

System.out.println("SQL Error: " + s.toString() + " "

+ s.getErrorCode() + " " + s.getSQLState());

} catch (Exception e) {

System.out.println("Error: " + e.toString()

+ e.getMessage());

}

}

[1229 byte] By [lofttya] at [2007-9-28 1:57:36]
«« help me!
»» jdk 1.5
# 1

I'm not sure I understand. It looks like you are getting info from the DB, not putting it in. Try using, ,

String update =

"update myTable set myField = '" + myValue + '" " +

"Where myTableValue = myCondition";

Statement.execute(update);

pauliemcneila at 2007-7-7 21:30:36 > top of Java-index,Archived Forums,Java Programming...
# 2

yes u r right sorry i put the wrong code in this is my code but it thows a genral error, why does this do this?

thanks ben

import java.sql.*;

class JD {

public static void main (String[] args) {

String data = "jdbc:odbc:World Energy";

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn = DriverManager.getConnection(data,"","");

Statement st = conn.createStatement();

st.executeUpdate("INSERT INTO Coal (FIPS, Country, Year)" +

"VALUES (gh, 'ghia', 1991)");

conn.close();

} catch (Exception e) {

System.err.println("Got an exception! ");

System.err.println(e.getMessage());

}

}

}

lofttya at 2007-7-7 21:30:36 > top of Java-index,Archived Forums,Java Programming...
# 3
Sounds like you are using Access. Your SQL statement is this:INSERT INTO Coal (FIPS, Country, Year) VALUES (gh, 'ghia', 1991)Is this a valid statement for your database? Type it into an Access query in SQL mode and see if it works.
DrClapa at 2007-7-7 21:30:36 > top of Java-index,Archived Forums,Java Programming...
# 4
thanks for the idea but i dount clearly know what you mean i have tried (which i think you mean but not to sure)thanks anyway for your time ben
lofttya at 2007-7-7 21:30:36 > top of Java-index,Archived Forums,Java Programming...