Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of createStatement in JDBC

 
Mukes Patel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[code=java][package com.jlcindia.jdbc;

import java.sql.*;

public class Lab1 {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbcdbc:JLCDSN", "system", "s2dio");
String sql = "insert into customers values(1,'sri','sri@jlc',123,'Blore');";
Statement st = con.createStatement();
int x = st.executeUpdate(sql);
if (x == 0) {
System.out.println("Record is not Inserted");
} else {
System.out.println("Rocord is Inserted");
}
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}
]
 
Mukes Patel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukes Patel wrote:[code=java][package com.jlcindia.jdbc;

import java.sql.*;

public class Lab1 {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbcdbc:JLCDSN", "system", "s2dio");
String sql = "insert into customers values(1,'sri','sri@jlc',123,'Blore');";
Statement st = con.createStatement();
int x = st.executeUpdate(sql);
if (x == 0) {
System.out.println("Record is not Inserted");
} else {
System.out.println("Rocord is Inserted");
}
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}
]
What is use of createStatement in this program? as it is used to Creates a Statemen object for sending SQL statements to the database. I mean to ask is it used to send String sql = "insert into customers alues(1,'sri','sri@jlc',123,'Blore');"; statement in the above program to the database. please clear my doubts.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Doc Reference:

createStatement()
Creates a Statement object for sending SQL statements to the database.

execute(String sql)
Executes the given SQL statement, which may return multiple results.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic