Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- public class Day27B {
- public static void main(String[] args) {
- testConnection();
- System.out.println("--------");
- insertRow1("Lily's");
- }
- static void testConnection() {
- String connString = "jdbc:mysql://localhost:3306/db_sg_b2_24";
- String userName = "root";
- String passWord = "";
- String sqlQuery = "SELECT COUNT(*)";
- try {
- Connection conn = DriverManager.getConnection(connString, userName, passWord);
- Statement stmt = conn.createStatement();
- boolean result = stmt.execute(sqlQuery);
- if (result) {
- System.out.println("SQL Query executed");
- }
- conn.close();
- } catch (Exception e) {
- System.out.println("error: " + e.toString());
- }
- }
- static void insertRow1(String txtIn) {
- String connString = "jdbc:mysql://localhost:3306/db_sg_b2_24";
- String userName = "root";
- String passWord = "";
- String sqlQuery = "INSERT INTO tbl_word_list(fld_word) VALUES ('" + txtIn + "')";
- try {
- Connection conn = DriverManager.getConnection(connString, userName, passWord);
- Statement stmt = conn.createStatement();
- int rowsAffected = stmt.executeUpdate(sqlQuery);
- if (rowsAffected == 1) {
- System.out.println("New Row Added");
- }
- conn.close();
- } catch (Exception e) {
- System.out.println("error: " + e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment