记一次java连接远程mysql数据库

Posted maoyq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记一次java连接远程mysql数据库相关的知识,希望对你有一定的参考价值。

用java连接远程数据库,写一个小java gui程序,写一张小表 student(number,name,sex),可以写入数据,删除数据,查询数据

主要代码:

  1 public class TestSqlOnFrame {
  2     public static void main(String[] args) {
  3         frame frameOne=new frame();
  4         frameOne.main();
  5     }
  6 }
  7 
  8 class frame extends Frame{
  9     //新建组件
 10     Frame frameone=new Frame("my java gui");
 11     
 12     TextField textOne=new TextField(5);
 13     TextField textTwo=new TextField(5);
 14     TextField textThree=new TextField(5);
 15     
 16     Label labelOne=new Label("number");
 17     Label labelTwo=new Label("name");
 18     Label labelThree=new Label("sex");
 19     
 20     Button buttonOne=new Button("set");
 21     Button buttonTwo=new Button("search");
 22     Button buttonThree=new Button("delete");
 23     Label labelForInformation=new Label();
 24     Font fconsole=new Font("Console", 0, 20);
 25 
 26     public void main() {
 27         //组件属性
 28         frameone.setLayout(new GridLayout(4,3));
 29         frameone.setSize(600,300);
 30         frameone.setLocation(300,300);
 31         frameone.setFont(fconsole);
 32         //组件监听器
 33         frameone.addWindowListener(new HandleWidows());
 34         buttonOne.addActionListener(new HandleButton());
 35         buttonTwo.addActionListener(new HandleButton());
 36         buttonThree.addActionListener(new HandleButton());
 37         //在框架里加入组件
 38         frameone.add(labelOne);
 39         frameone.add(labelTwo);
 40         frameone.add(labelThree);
 41         frameone.add(textOne);
 42         frameone.add(textTwo);
 43         frameone.add(textThree);
 44         frameone.add(buttonOne);
 45         frameone.add(buttonTwo);
 46         frameone.add(buttonThree);
 47         frameone.add(labelForInformation);
 48         frameone.add(new Label());
 49         frameone.add(new Label());
 50         frameone.setVisible(true);
 51         
 52     }
 53     //内部类监听动作
 54     class HandleButton implements ActionListener{
 55         public void actionPerformed(ActionEvent e) {
 56             try {
 57                 //新建驱动,注册驱动
 58                 com.mysql.jdbc.Driver driverOne=new com.mysql.jdbc.Driver();
 59                 DriverManager.registerDriver(driverOne);
 60                 //连接的三个参数,建立连接
 61                 String url="JDBC:Mysql://你的数据库服务器ip地址:3306/你的数据库名";
 62                 String name="你的用户名";
 63                 String password="你的密码";
 64                 Connection conn=DriverManager.getConnection(url,name,password);
 65                 //建立sql信使:
 66                 Statement st=(Statement) conn.createStatement();
 67                 String sql="";
 68                 //resultset用来接收查询
 69                 ResultSet rs;
 70                 //按钮动作,分别是insert select delete
 71                 if (e.getSource()==buttonOne) {
 72                     //add to database
 73                     String numberWillBeAdded=textOne.getText();
 74                     sql="insert into student values("+textOne.getText()
 75                         +",‘"+textTwo.getText()+"‘"
 76                         +",‘"+textThree.getText()
 77                         +"‘);";
 78                     System.out.println(sql);
 79                     st.executeUpdate(sql);
 80                     labelForInformation.setText(numberWillBeAdded+" added");
 81                 }
 82                 if (e.getSource()==buttonTwo) {
 83                     //search
 84                     sql="select * from student where number"
 85                             +"="+textOne.getText()+";";
 86                     rs=st.executeQuery(sql);
 87                     System.out.println(sql);
 88                     if (rs.next() == true) {
 89                         labelForInformation.setText("find it!");
 90                     }else{
 91                         labelForInformation.setText("fail to find it");
 92                     }
 93                     textTwo.setText("");
 94                     textThree.setText("");
 95                     textTwo.setText(rs.getString("name"));
 96                     textThree.setText(rs.getString("sex"));
 97                 }
 98                 if (e.getSource()==buttonThree) {
 99                     //delete
100                     String numberWillBeDeteled=textOne.getText();
101                     sql="delete from student where number="
102                             +textOne.getText()+";";
103                     st.executeUpdate(sql);
104                     labelForInformation.setText(numberWillBeDeteled+" deleted");
105                 }
106             } catch (SQLException e1) {
107                 // TODO: handle exception
108             }
109         }
110     }
111     class HandleWidows extends WindowAdapter{
112          public void windowClosing(WindowEvent e) {
113              System.exit(0);
114          }
115     }
116     
117 }

当然,记得下好mysql的驱动然后加入build path

 


以上是关于记一次java连接远程mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章

记一次网络原因导致的mysql连接中断问题(druid)

记一次优化的的意外收获

记一次排查mysql数据库连接未关闭问题的过程

记一次SSM项目小结

记一次mysql磁盘io高的问题排查

记一次无法远程故障排查20161211.2111