0% found this document useful (0 votes)
15 views2 pages

How To DoJDBCunderCMD

This document provides steps to connect to a MySQL database and execute a simple INSERT statement from the command line using JDBC in Java. The steps are: 1) Add the MySQL JDBC driver JAR file to the classpath. 2) Start the MySQL server. 3) Create a sample database and table. 4) Set the Java classpath and path environment variables. 5) Compile and run a Java program that connects to the database using JDBC, executes an INSERT statement, and closes the connection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

How To DoJDBCunderCMD

This document provides steps to connect to a MySQL database and execute a simple INSERT statement from the command line using JDBC in Java. The steps are: 1) Add the MySQL JDBC driver JAR file to the classpath. 2) Start the MySQL server. 3) Create a sample database and table. 4) Set the Java classpath and path environment variables. 5) Compile and run a Java program that connects to the database using JDBC, executes an INSERT statement, and closes the connection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

How to do JDBC under CMD

1) Copy mysql-connector-java-5.1.39-bin.jar file in e:\jmvsp

2) start mysql.exe from c:\mysql\bin

3) create test Database with employee table.[you may use mysqlGUItool


QueryBrowser]

Then in CMD do the following:

Z:\>c:

C:\>set path=.;c:\;C:\Program Files (x86)\Java\jdk1.7.0_45\bin

C:\>set classpath=e:\;.;e:\jmvsp

C:\> E:

E:\jmvsp>

E:\jmvsp>javac jdbc2.java

E:\jmvsp>java -cp mysql-connector-java-5.1.39-bin.jar; jdbc2

Connection Established

Data Inserted

connection closed

E:\jmvsp>

Now check table records for confirmation.

Program jdbc2.java is given below:

import java.sql.*;

public class jdbc2 { final static String URL =


"jdbc:mysql://localhost:3306/test";

final static String USER = "root"; final static String PASS = "";
static Connection con = null; static Statement stmt = null;

public static void main(String[] args) {

try { // other mysql driver is com.mysql.jdbc.Driver or


org.gjt.mm.mysql.Driver

String qry ="INSERT INTO test.employee


Values(3,'peri','sastry','india')";

// Above, change primary key value and other table cell values as
required
Class.forName("com.mysql.jdbc.Driver");

con= DriverManager.getConnection(URL,USER,PASS);

System.out.println("Connection Established");

stmt = con.createStatement(); stmt.executeUpdate(qry);

System.out.println("Data Inserted"); con.close();

} catch(ClassNotFoundException e) { e.printStackTrace();

} catch(SQLException e) { e.printStackTrace(); }

finally { System.out.println("connection closed");} } }

You might also like