Docsity

Java MySQL Database Connection Class, Lecture notes of Java Programming

Toggle sidebar
bg1
package bean;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.*;
/**
*
* @author labs
*/
public class dbConn {
private Connection con = null;
private String url = "jdbc:mysql://localhost:3306/";
private String db = "event";
private String driver = "com.mysql.jdbc.Driver";
private String user = "root";
private String pass = "";
private String id = "";
private String title = "";
private String schedule = "";
private String scheduleBy = "";
private String description = "";
public dbConn(){}
public void setConnection() throws Exception
{
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url + db, user, pass);
}catch (Exception e) {
e.printStackTrace();
}
}
public void search() throws Exception
{
try{
setConnection();
ResultSet rs = null;
Statement stmt = con.createStatement();
rs = stmt.executeQuery("Select * From event where `title`
= "+ title+" || `date` = "+ schedule);
if(rs.next())
{
setId(rs.getString(0));
setTitle(rs.getString(1));
setSchedule(rs.getString(2));
setScheduleBy(rs.getString(3));
setDescription(rs.getString(4));
}
docsity.com
pf3

Partial preview of the text

Download Java MySQL Database Connection Class and more Lecture notes Java Programming in PDF only on Docsity!

package bean;

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor. / import java.sql.; /**
  • @author labs */ public class dbConn { private Connection con = null; private String url = "jdbc:mysql://localhost:3306/"; private String db = "event"; private String driver = "com.mysql.jdbc.Driver"; private String user = "root"; private String pass = ""; private String id = ""; private String title = ""; private String schedule = ""; private String scheduleBy = ""; private String description = "";

public dbConn(){}

public void setConnection() throws Exception { try { Class.forName(driver).newInstance(); con = DriverManager.getConnection(url + db, user, pass); }catch (Exception e) { e.printStackTrace(); } }

public void search() throws Exception { try{ setConnection(); ResultSet rs = null;

Statement stmt = con.createStatement(); rs = stmt.executeQuery("Select * From event where title = "+ title+" || date = "+ schedule);

if(rs.next()) { setId(rs.getString(0)); setTitle(rs.getString(1)); setSchedule(rs.getString(2)); setScheduleBy(rs.getString(3)); setDescription(rs.getString(4)); }

catch(Exception ex) { System.out.println(ex.getMessage()); } }

public void setUrl(String url){this.url = url;} public void setDb(String db){this.db = db;} public void setDriver(String driver){this.driver = driver;} public void setUser(String user){this.user = user;} public void setPass(String pass){this.pass = pass;} public void setId(String id){this.id = id;} public void setTitle(String title){this.title = title;} public void setSchedule(String schedule){this.schedule = schedule;} public void setScheduleBy(String scheduleBy){this.scheduleBy = scheduleBy;} public void setDescription(String description){this.description = description;}

public String getUrl(){return url;} public String getDb(){return db;} public String getDriver(){return driver;} public String getUser(){return user;} public String getPass(){return pass;} public String getId(){return id;} public String getTitle(){return title;} public String getSchedule(){return schedule;} public String getScheduleBy(){return scheduleBy;} public String getDescription(){return description;} /*public int searchFilmByTitle(String title) { try { setConnection(); film_id=0; Statement st = con.createStatement(); String stmt = "SELECT * FROM film where title= '" + title + "'"; System.out.println(stmt); ResultSet res = st.executeQuery(stmt); while (res.next()) { film_id=res.getInt("film_id"); } System.out.println("film_id: "+film_id); con.close();

} catch (SQLException s) { System.out.println("SQLException:.. " + s.getMessage()); } catch (Exception s) { System.out.println("Exception:.. " + s.getMessage());