Java Reference
In-Depth Information
Let us see a sample code here:
import java.sql.*;
public class database_insert {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/database_name";
static final String USER = "username";
static final String PASS = "password";
public static void main(String args[]) {
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO <table name> " +
"VALUES (100, x, y, z)";
stmt.executeUpdate(sql);
if(stmt!=null)
conn.close();
}
}
Let us see what we have written and how we completed all the steps we mentioned about
database programming.
Import database packages - We have imported database packages in the in-
clude java.sql.* statement.
JDBC/ODBC driver registration - we have registered the database driver to
a mySQL database engine through the JDBC DRIVER, DB URL & USER
NAME.
Open a connection -We have opened a connection to the database using the
connection string.
Execute a query - We have executed a query to make an insert operation in
the database.
Search WWH ::




Custom Search