import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSet;
class db
	{
		public static void main(String arg[])
			{
				Statement statementObj=null;
				Connection connectionObj=null;
				try
					{
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        			connectionObj=DriverManager.getConnection("jdbc:odbc:sensorData","","");
			                        statementObj=connectionObj.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
						System.out.println("do not worry ho ga yr ho ga");
						statementObj.executeUpdate("INSERT INTO hardwareData (Time, Date, humidity, Temp, DiesalLevel) VALUES('1','12','3','s','a')");
						connectionObj.close();
					}
				catch(ClassNotFoundException e)
					{
						System.out.println("class not found exception");
					}
				
				catch(SQLException e)
					{
						e.printStackTrace();
					}
			}   
       }

Recommended Answers

All 10 Replies

which database Engine you want to connect

Please put your code snippets within

blocks in the future - it makes it easier for us all to read.

Can you post your exception stack so we can see what might be going wrong?

Maybe in the database your using. The TIME and DATE are reserved keywords that you should escape first. for sql, [TIME],[DATE]. For mysql, `TIME`,`DATE`

I m using MS access

it shows
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in
INSERT INTO statement.
And it all the field of data base is Text.

Maybe in the database your using. The TIME and DATE are reserved keywords that you should escape first. for sql, [TIME],[DATE]. For mysql, `TIME`,`DATE`

if i use this Statement:
statementObj.executeUpdate("INSERT INTO hardwareData VALUES('1','12','3','s','a')");
where in database fields are
Time:Text
Date:Text
humidity:Text
Temp:Text
DiesalLevel:Text

If you use that statement, what? The machine blows up?

INSERT INTO hardwareData ([Time], [Date], humidity, Temp, DiesalLevel) VALUES('1','12','3','s','a')
INSERT INTO hardwareData ([Time], [Date], humidity, Temp, DiesalLevel) VALUES('1','12','3','s','a')

First of all THANK YOU. My code is working.
Can you tell me please why we required square bracket in Time and Date field

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.