Parcel? cn . hpu . BBS . util;
Import? Java . SQL . connection;
Import? Java . SQL . driver manager;
Import? Java . SQL . prepared statement;
Import? Java . SQL . resultset;
Import? Java . SQL . SQL exception;
Import? Java.sql. statement;
Public? Class? DB? {
//? Defining database drivers for MySQL
Public? Static electricity Final? String? DBDRIVER? =? “com.mysql.jdbc.Driver”? ;
//Define the database connection address of mysql:
Public? Static electricity Final? String? DBDURL? =? " JDBC:MySQL://localhost/BBS 20 14 "? ;
The connection user name of //mysql database//
Public? Static electricity Final? String? DBUSER? =? "Root"? ;
//mysql database connection password
Public? Static electricity Final? String? DBPASS? =? " 1234"? ;
Public? Static electricity Contact? createConn(){
Contact? Kang En? = null
Try it? {
class . forname(DBDRIVER);
conn = driver manager . getconnection(db durl,DBUSER,db pass);
}? Catch? (ClassNotFoundException? e)? {
e . printstacktrace();
}? Catch? (SQLException? e)? {
e . printstacktrace();
}
Return? conn
}
Public? Static electricity A prepared report? Prepare (connect? Control room, string? sql){
A prepared report? ps = null
Try it? {
PS = conn . prepare statement(SQL);
}? Catch? (SQLException? e)? {
//? Todo? Automatically generated? Catch? block
e . printstacktrace();
}
Return? PS;
}
Public? Static electricity Invalid? Close (connect? Connect) {
if(conn==null)? Return;
Try it? {
conn . close();
conn = null
}? Catch? (SQLException? e)? {
//? Todo? Automatically generated? Catch? block
e . printstacktrace();
}
}
Public? Static electricity Invalid? Close (declare? stmt){
if(stmt==null)? Return;
Try it? {
stmt . close();
stmt = null
}? Catch? (SQLException? e)? {
//? Todo? Automatically generated? Catch? block
e . printstacktrace();
}
}
Public? Static electricity Invalid? Close (ResultSet? rs){
if(rs==null)? Return;
Try it? {
RS . close();
rs = null
}? Catch? (SQLException? e)? {
//? Todo? Automatically generated? Catch? block
e . printstacktrace();
}
}
} a JavaBean belonging to the} category:
Parcel? cn . hpu . BBS . model;
Public? Class? Category? {
Private? int? id;
Private? String? Name;
Private? String? Description;
Public? int? getId()? {
Return? id;
}
Public? Invalid? setId(int? id)? {
This. id? =? id;
}
Public? String? getName()? {
Return? Name;
}
Public? Invalid? SetName (string? Name)? {
This name? =? Name;
}
Public? String? getDescription()? {
Return? Description;
}
Public? Invalid? SetDescription (string? Description)? {
This. Description? =? Description;
}
} Operation classes for databases and categories://To put it bluntly, it means adding, deleting, querying and repairing.
& ltpre? name="code "? Class = "java"> package? cn . hpu . BBS . service;
Import? Java . SQL . connection;
Import? Java . SQL . prepared statement;
Import? Java . SQL . resultset;
Import? Java . SQL . SQL exception;
Import? Java . util . ArrayList;
Import? Java . util . list;
Import? cn . hpu . BBS . model . category;
Import? cn . hpu . BBS . util . db;
Public? Class? CategoryService? {
Public? Invalid? Add (category? c){
Contact? conn = db . create conn();
String? sql="insert? Become? Category? (name, description)? Values? (? ,? )";
A prepared report? ps=DB.prepare(conn,SQL);
Try it? {
ps.setString( 1,? c . getname());
ps.setString(2,? c . get description());
PS . execute update();
}? Catch? (SQLException? e)? {
e . printstacktrace();
}
db . close(PS);
DB.close (connection);
}
Public? List & lt category & gt? list(){
Contact? conn = db . create conn();
String? sql="select? *? From where? Category ";
A prepared report? ps=DB.prepare(conn,SQL);
List & lt category & gt? Category = New? ArrayList & lt category & gt ();
Try it? {
Result set? RS = PS . execute query();
Category? C = empty;
while(rs.next()){
C = new? Category ();
c . setid(RS . getint(" id "));
c . setname(RS . getstring(" name "));
c . set description(RS . getstring(" description "));
categories . add(c);
}
}? Catch? (SQLException? e)? {
e . printstacktrace();
}
db . close(PS);
DB.close (connection);
Return? Category;
}
Public? Invalid? Delete (category? c){
delete byid(c . getid());
}
Public? Invalid? deleteById(int? id){
Contact? conn = db . create conn();
String? Sql= "Delete? From where? Category? Where is it? id=? ”;
A prepared report? ps=DB.prepare(conn,SQL);
Try it? {
ps.setInt( 1,? id);
PS . execute update();
}? Catch? (SQLException? e)? {
e . printstacktrace();
}
db . close(PS);
DB.close (connection);
}
Public? Invalid? Update (category? c){
Contact? conn = db . create conn();
String? Sql= "Update? Category? Settings? Name? =? ,? Description? =? Where is it? id? =";
A prepared report? ps=DB.prepare(conn,SQL);
Try it? {
ps.setString( 1,? c . getname());
ps.setString(2,? c . get description());
ps.setInt(3,? c . getid());
PS . execute update();
}? Catch? (SQLException? e)? {
e . printstacktrace();
}
db . close(PS);
DB.close (connection);
}
Public? Category? loadById(int? id){
Contact? conn = db . create conn();
String? sql="select? *? From where? Category? Where is it? id=? ”;
A prepared report? ps=DB.prepare(conn,SQL);
Category? C = empty;
Try it? {
ps.setInt( 1,? id);
Result set? RS = PS . execute query();
if(rs.next()){
C = new? Category ();
c . setid(RS . getint(" id "));
c . setname(RS . getstring(" name "));
c . set description(RS . getstring(" description "));
}
}? Catch? (SQLException? e)? {
e . printstacktrace();
}
db . close(PS);
DB.close (connection);
Return? c;
}
}