Java DatBase Connectivity help

keshara

Well-known member
  • Sep 20, 2006
    2,039
    418
    83
    package controller;

    import com.mysql.jdbc.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    public class db {

    static Connection c;

    public static void createConnection() throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3307/dbname", "root", "password");


    }

    public static void iud(String s) throws Exception {
    if (c == null) {
    createConnection();
    }

    Statement stmt = c.createStatement();
    stmt.executeUpdate(s);

    }

    public static ResultSet search(String s) throws Exception {

    if (c == null) {
    createConnection();
    }
    Statement stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery(s);
    return rs;

    }
    }