I. First create a class that encapsulates the database and personal information:
Package Com.yxq.dao;
public class Constants {
public static String URL = "Jdbc:mysql://127.0.0.1:3306/personblog";
public static String classname= "Com.mysql.jdbc.Driver";
public static String UserName = "root";
public static String password = "root";
}
Just modify the information of this class later;
Also add the jar package to the project;
Second, encapsulate a class to connect to the database and close the database:
Package Com.yxq.dao;
/* Import the required packages */
Import java.sql.connection;//represents a link to a database;
Import java.sql.drivermanager;//the class used to get the database link;
Import java.sql.resultset;//encapsulates the class of the query result set;
Import java.sql.SQLException;
Import java.sql.Statement; The class used to execute the SQL statement;
public class DB {
//access to data connection;
public static Connection getconnection () throws exception{
try{
Class.forName (Constants.classname);
Connection con = drivermanager.getconnection (Constants.url,constants.username,constants.password);
return con;
}catch (Exception e) {
E.printstacktrace ();
System.out.println ("Connection failed");
return null;
}
}
//Close statement object;
public static void Closestatement (Statement stmt) throws exception{
try{
if (stmt!=null) {
Stmt.close ();
SYSTEM.OUT.PRINTLN ("Close statement success");
}
}catch (Exception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("shutdown statement failed");
}
}
//Close ResultSet object;
public static void Closeresultset (ResultSet rs) throws exception{
try{
if (rs!=null) {
Rs.close ();
System.out.println ("Close resultset success");
}
}catch (Exception e) {
E.printstacktrace ();
System.out.println ("Shutdown resultset failed");
}
}
//Close connection object;
public static void CloseConnection (Connection conn,statement stm,resultset rs) throws exception{
try{
if (rs!= null) {
Rs.close ();
System.out.println ("Close RS success");
}
if (STM! = null) {
Stm.close ();
SYSTEM.OUT.PRINTLN ("Close STM success");
}
if (conn!=null) {
Conn.close ();
SYSTEM.OUT.PRINTLN ("Close connection success");
}
}catch (Exception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Shutdown Connection failed");
}
}
}
Three. Create a specific class, to delete or modify the data in a particular table;
Package Com.yxq.dao;
Import java.sql.Connection;
Import Java.sql.Date;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;
Import java.util.List;
Import com.yxq.valuebean.Model_Artical;
Import Com.yxq.dao.DB;
public class Articaldao {
/*
* @Description: Search for articles;
* @param: The parameter is Start,end, is the data of the first data, and the number of one query;
* @return: Returns the record of the query, encapsulated in the list.
* */
Public list<model_artical> query (int start,int end) throws exception{
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
String sql= "SELECT * from tbl_artical limit" +start+ "," +end+ "";
ResultSet rs= stm.executequery (SQL);
list<model_artical> model_artical = new arraylist<model_artical> ();
Model_artical artical= null;
while (Rs.next ()) {
artical = new Model_artical ();
ARTICAL.SETARTICAL_ID (Rs.getint (1));
Artical.setartical_name (rs.getstring ("Artical_name"));
Artical.setartical_content (rs.getstring ("artical_content"));
Artical.setartical_date (Rs.getdate ("artical_date"));
Artical.setartical_glancenumber (Rs.getint ("Artical_glancenumber"));
ARTICAL.SETUSER_ID (Rs.getint ("user_id"));
ARTICAL.SETARTICALTYPEL_ID (Rs.getint ("articaltypel_id"));
Artical.setartical_character (rs.getstring ("Artical_character"));
Model_artical.add (artical);
}
Db.closeconnection (CON,STM,RS);
return model_artical;
}
/*
* @Description: Search for articles;
* @param: The parameter is Typeid,start,end, is the typeid value of the article and the data of the first data, and the number of one query;
* @return: Returns the record of the query, encapsulated in the list.
* */
Public list<model_artical> querytype (int typeid,int start,int end) throws exception{
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
String sql= "SELECT * from tbl_artical where articaltypel_id = '" +typeid+ "' Limit" +start+ "," +end+ "";
ResultSet rs= stm.executequery (SQL);
list<model_artical> model_artical = new arraylist<model_artical> ();
Model_artical artical= null;
while (Rs.next ()) {
artical = new Model_artical ();
ARTICAL.SETARTICAL_ID (Rs.getint (1));
Artical.setartical_name (rs.getstring ("Artical_name"));
Artical.setartical_content (rs.getstring ("artical_content"));
Artical.setartical_date (Rs.getdate ("artical_date"));
Artical.setartical_glancenumber (Rs.getint ("Artical_glancenumber"));
ARTICAL.SETUSER_ID (Rs.getint ("user_id"));
ARTICAL.SETARTICALTYPEL_ID (Rs.getint ("articaltypel_id"));
Artical.setartical_character (rs.getstring ("Artical_character"));
Model_artical.add (artical);
}
Db.closeconnection (CON,STM,RS);
return model_artical;
}
/* Create a method for viewing the details of the article---queryarticlesingle ();
* This method has a parameter. Indicates the id* of the article
*/
Public model_artical queryarticlesingle (int id) throws exception{
String sql = "SELECT * from tbl_artical where artical_id = '" +id+ "'";
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
ResultSet rs= stm.executequery (SQL);
try{
while (Rs.next ()) {
model_artical artical = new model_artical ();
ARTICAL.SETARTICAL_ID (Rs.getint (1));
Artical.setartical_name (rs.getstring ("Artical_name"));
Artical.setartical_content (rs.getstring ("artical_content"));
Artical.setartical_date (Rs.getdate ("artical_date"));
Artical.setartical_glancenumber (Rs.getint ("Artical_glancenumber"));
ARTICAL.SETUSER_ID (Rs.getint ("user_id"));
ARTICAL.SETARTICALTYPEL_ID (Rs.getint ("articaltypel_id"));
Db.closeconnection (CON,STM,RS);
return artical;
}
}catch (Exception e) {
E.printstacktrace ();
System.out.println ("View articles by article category failed");
Db.closeconnection (CON,STM,RS);
return null;
}
return null;
}
/* Operationartical () method implements additional update operations to the database, such as adding, modifying, and deleting * /
public boolean operattionartical (String oper,model_artical artical) throws exception{
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
String sql = null;
if (Oper.equals ("add")) {
sql = "INSERT INTO tbl_artical (artical_name,artical_content,artical_date,artical_glancenumber,articaltypel_id,user _id,artical_character) VALUES (' "+artical.getartical_name () +" ', ' "+artical.getartical_content () +" ', ' "+new Date ( Artical.getartical_date (). GetTime ()) + "', '" +artical.getartical_glancenumber () + "', '" +artical.getarticaltypel_id ( ) + "', '" +artical.getuser_id () + "', '" +artical.getartical_character () + "')";
int rs = stm.executeupdate (SQL);
Db.closeconnection (Con,stm,null);
if (rs > 0) {
return true;
}else{
return false;
}
}
return false;
}
/ * Create a method to delete the corresponding article * /
public boolean deleteartical (int articalid) throws exception{
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
String sql= "Delete from tbl_artical where artical_id =" +articalid;
int rs = stm.executeupdate (SQL);
if (rs > 0) {
Db.closeconnection (Con,stm,null);
System.out.println ("delete succeeded");
return true;
}else{
Db.closeconnection (Con,stm,null);
System.out.println ("delete failed");
return false;
}
}
/ * Create an article that corresponds to a change * /
public boolean modifyarticle (int articleid,string articlecontent,string articlename,string character,int typeid) Throws exception{
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
String sql= "UPDATE tbl_artical SET artical_content = '" +articlecontent+ "', Artical_name= '" +articlename+ "', Artical_ Character = ' "+character+" ', articaltypel_id= "+typeid+" where artical_id = "+articleid;
int rs = stm.executeupdate (SQL);
if (rs > 0) {
Db.closeconnection (Con,stm,null);
SYSTEM.OUT.PRINTLN ("modified successfully");
return true;
}else{
Db.closeconnection (Con,stm,null);
SYSTEM.OUT.PRINTLN ("modification failed");
return false;
}
}
/*
* @Description: Calculates the total number of articles or articles in a category;
* @param: Parameters are typeID, judging is a certain kind of article, or all the articles in the database,
* @return: return quantity;
* */
public int articalnum (int typeid) throws exception{
String SQL;
if (typeID > 0) {
sql = "SELECT COUNT (*) from tbl_artical where articaltypel_id = '" +typeid+ "'";
}else{
Sql= "SELECT COUNT (*) from tbl_artical";
}
Connection con = db.getconnection ();
Statement stm = Con.createstatement ();
ResultSet rs= stm.executequery (SQL);
int i = 0;
while (Rs.next ()) {
i = Rs.getint (1);
}
Db.closeconnection (CON,STM,RS);
return i;
}
}
Project Connection database, class encapsulation;