简单的Java客户端程序:MySQL+Java+JFrame
这是一本Java书中的课后习题,我自己写出来的和书上给的参考代码有差别
題目的內容是:
完成一个博客发布程序,C/S结构,包含博客标题,内容,作者,积分
在更新数据库时需要考虑到回滚机制
首先是确定数据结构,分为文章部分和积分部分,要求是使用两个表来实现,即两个表可以用两个类来表示:
Jifen.java
public class Jifen { public int fid,value,blogID,type; public Jifen(int value,int type) { this.value=value; this.type=type; } public Jifen(int fid,int value,int blogID,int type) { this.fid=fid; this.value=value; this.blogID=blogID; this.type=type; } public int getFid() { return fid; } public void setFid(int fid) { this.fid=fid; } public int getValue() { return value; } public void setValue(int value) { this.value=value; } public int getBlogID() { return blogID; } public void setBlogID(int blogID) { this.blogID=blogID; } public int getType() { return type; } public void setType(int type) { this.type=type; } }
Blog.java
public class Blog { int ID; String title; String author; String content; Jifen jf; public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author=author; } public String getContent() { return content; } public void setContent(String content) { this.content=content; } public Jifen getJf() { return jf; } public void setJF(Jifen jf) { this.jf=jf; } public Blog(int ID,String title,String author,String content,Jifen jf) { this.ID=ID; this.title=title; this.author=author; this.content=content; this.jf=jf; } public Blog(String title,String author,String content,Jifen jf) { this.title=title; this.author=author; this.content=content; this.jf=jf; } }
然后需要一个类来读写数据库,使用MySQL数据库
在使用数据库之前得新建数据库以及表,判断机制为没有则新建,有就不进行任何操作
以上我均将其放在构造函数中来实现
实现以上之后,再实现一个插入程序,分两次插入,首先插入文章,再插入积分,如果未完成则回滚撤销插入。
具体代码如下:
BlogDatabase.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mysql.jdbc.PreparedStatement; /* * 先创建数据库 * 然后新建数据表 */ public class BlogDatabase { String drivers = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String user = ""; String password = ""; String databaseName = "blogdata"; String myBlogSQL = "create table myblog(" + "id integer primary key auto_increment," + "title varchar(20)," + "content varchar(2000)," + "author varchar(20))"; String myPointsSQL = "create table points(" + "fid integer primary key auto_increment," + "value integer," + "blogID integer," + "type integer)"; Connection conn; public BlogDatabase() { Statement st = null; try { Class.forName(drivers); conn = DriverManager.getConnection(url, user, password); st = conn.createStatement(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (createDatabase(st)) { System.out.println("database create succeed"); } else { System.out.println("database exited"); } if (createTable(st, myBlogSQL)) { System.out.println("myblog create succeed"); } else { System.out.println("table create failed"); } if (createTable(st, myPointsSQL)) { System.out.println("points create succeed"); } else { System.out.println("table create failed"); } try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean createDatabase(Statement st) { boolean temp = false; String createSQL = "create database " + databaseName; try { st.execute(createSQL); temp = true; } catch (Exception e) { // TODO: handle exception temp = false; } try { st.execute("use " + databaseName); } catch (Exception e) { // TODO: handle exception } return temp; } public boolean createTable(Statement st, String mSql) { boolean temp = false; try { st.execute(mSql); temp = true; } catch (Exception e) { // TODO: handle exception temp = false; } return temp; } public int insert(Blog b) throws SQLException { int temp = 0; Statement st = null; conn.setAutoCommit(false); try { st = conn.createStatement(); if (insertBlog(b, st)) { if (insertPoints(b, st)) { temp = 2; } else { conn.rollback(); temp = 3; } } else { conn.rollback(); temp = 1; } } catch (Exception e) { // TODO: handle exception temp = 0; } try { st.close(); conn.setAutoCommit(true); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return temp; } public boolean insertBlog(Blog b, Statement st) { boolean temp = false; try { String msql = "insert into myblog(title,content,author) " + "values('" + b.getTitle() + "','" + b.getContent() + "','" + b.getAuthor() + "')"; if (st.executeUpdate(msql) == 1) { temp = true; } else { temp = false; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); temp = false; } return temp; } public boolean insertPoints(Blog b, Statement st) { boolean temp = false; try { Jifen jf = b.getJf(); String msql = "insert into points(value,blogID,type) " + "values(" + jf.getValue() + "," + jf.getBlogID() + "," + jf.getType() + ")"; if (st.executeUpdate(msql) == 1) { temp = true; } else { temp = false; } } catch (Exception e) { // TODO: handle exception temp = false; } return temp; } }
接下来就是实现界面编程,将其封装成一个类:
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class MyBlogWin { JFrame win=new JFrame("My Blog"); JPanel north=new JPanel(); JTextField title=new JTextField(10); JTextField author=new JTextField(10); JTextField jifen=new JTextField(10); JPanel south=new JPanel(); JButton sub=new JButton("sumbit"); JButton clear=new JButton("clear"); JTextArea jt=new JTextArea(10,10); JScrollPane js=new JScrollPane(jt); JLabel tip=new JLabel(""); BlogDatabase blogDatabase=new BlogDatabase(); public MyBlogWin() { north.add(new JLabel("title")); north.add(title); north.add(new JLabel("author")); north.add(author); north.add(new JLabel("jifen")); north.add(jifen); win.add(BorderLayout.NORTH,north); win.add(js); sub.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { String stitle=title.getText().trim(); String scontent=jt.getText().trim(); String sauthor=author.getText().trim(); String sjifen=jifen.getText().trim(); int jf=Integer.parseInt(sjifen); System.out.println("title:"+stitle+",content:"+scontent+",author:"+sauthor+",jifen:"+sjifen); Jifen jif=new Jifen(jf, 1); Blog b=new Blog(stitle, sauthor, scontent, jif); int temp=blogDatabase.insert(b); if(temp==1) { }else if(temp==2) { }else if(temp==3) { }else if(temp==0) { } System.out.println(temp); }catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); tip.setText("failed"); } } }); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub jt.setText(""); } }); south.add(tip); south.add(sub); south.add(clear); win.add(BorderLayout.SOUTH,south); win.pack(); win.setVisible(true); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
然后在主程序中
MyBlogWin my=new MyBlogWin();
就可以实现以上的功能