Skip to content

Instantly share code, notes, and snippets.

@cankush625
Created May 25, 2021 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cankush625/a8b0709f737be15a3d41479d86ea3b74 to your computer and use it in GitHub Desktop.
Save cankush625/a8b0709f737be15a3d41479d86ea3b74 to your computer and use it in GitHub Desktop.
package jdbc;
import java.sql.*;
public class MysqlConnect {
public static void main(String[] args ) {
System.out.println("Inserting values in mysql database table!");
Connection conn = null;
String url = "jdbc:mysql://192.168.43.6";
String db = "learntrix";
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url + db, "root", "ankush");
try {
Statement st = conn.createStatement();
String sql = "insert into jdbctest.employee"
+ " values(" + 3 + "," +
"'ABC1'" + "," +
"'Ankush Chavan'" + "," +
"20000)";
System.out.println(sql);
int val = st.executeUpdate(sql);
System.out.println("1 row affected + return value: " + val);
} catch(SQLException s) {
System.out.println("SQL statement is not executable! Error is: " + s.getMessage());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment