码迷,mamicode.com
首页 > 数据库 > 详细

jdbc链接MySQL

时间:2018-04-04 15:03:51      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:com   value   exe   lock   case   tac   auto   else   set   

  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.PreparedStatement;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.sql.Statement;
  7 import java.sql.Timestamp;
  8 import java.util.Scanner;
  9 
 10 public class jdbcDemo {
 11 
 12     static Scanner  input=new Scanner(System.in);      //静态的Scanner 方便给用输入
 13    static String jdbc="jdbc:mysql://localhost:3306/t22?useUnicode=true&characterEncoding=utf-8";
 14    static String Driver="com.mysql.jdbc.Driver";
 15    
 16     public static void main(String[] args) {
 17                        
 18                     myjdbc();  //调用执行jdbc增删改查
 19     }
 20     private static void myjdbc() {
 21         System.out.println("-------------------进入jdbc增删改查----------------------");
 22         System.out.println("请输入你要操作的序号");
 23         System.out.println("-------注册/1---------");
 24         System.out.println("-------登录/2---------");
 25         System.out.println("-------退出/3---------");
 26         int serial=input.nextInt(); //输入1进入注册 2 登录
 27         switch (serial) {
 28         case 1:
 29             register();  //注册
 30             break;
 31         case 2:
 32             logon();     //登录
 33             break;
 34             case 3:
 35                 System.out.println("欢迎下次再来");
 36             System.exit(0);  //退出
 37             break;
 38         default:
 39             myjdbc();  //回到一级菜单
 40             break;
 41         }
 42         
 43         
 44     }
 45     
 46     //登录
 47     private static void logon() {//方便关闭资源
 48         Connection conn=null;  
 49         Statement stmt=null;
 50         ResultSet rs = null;
 51 
 52         try {
 53             //加载驱动
 54             Class.forName(Driver);
 55         } catch (ClassNotFoundException e) {
 56             // TODO Auto-generated catch block
 57             e.printStackTrace();
 58         }
 59         try {
 60             conn=DriverManager.getConnection(jdbc,"epetamin","1234");
 61             stmt=conn.createStatement();
 62             System.out.println("请输入登录名");
 63             String Names=input.next();
 64             System.out.println("请输入密码");
 65             int pas=input.nextInt();
 66             //要登录的数据
 67             String sql="select *from "+Names+" where id="+pas+"";
 68             rs=stmt.executeQuery(sql);
 69             if(rs.next()){
 70                 System.out.println("登陆成功");
 71                 second();
 72             }else{
 73                 System.out.println("登陆失败");
 74                 myjdbc();
 75             }
 76         } catch (SQLException e) {
 77             // TODO Auto-generated catch block
 78             e.printStackTrace();
 79         }finally{                //关闭资源
 80             if(rs!=null){
 81                 try {
 82                     rs.close();
 83                 } catch (SQLException e) {
 84                     // TODO Auto-generated catch block
 85                     e.printStackTrace();
 86                 }
 87             }
 88             if(stmt!=null){
 89                 try {
 90                     stmt.close();
 91                 } catch (SQLException e) {
 92                     // TODO Auto-generated catch block
 93                     e.printStackTrace();
 94                 }
 95             }
 96             if(conn!=null){
 97                 try {
 98                     conn.close();
 99                 } catch (SQLException e) {
100                     // TODO Auto-generated catch block
101                     e.printStackTrace();
102                 }
103             }
104             
105         }
106         
107     }
108     
109     //注册
110     private static void register() {
111         Connection conn=null;
112         Statement stmt=null;
113         try {
114             //加载驱动
115             Class.forName(Driver);
116         } catch (ClassNotFoundException e) {
117             // TODO Auto-generated catch block
118             e.printStackTrace();
119         }
120                 try {
121                     
122                     conn=DriverManager.getConnection(jdbc,"epetamin","1234");
123                     stmt=conn.createStatement();
124                     System.out.println("请输入姓名");
125                     String name=input.next();
126                     System.out.println("请输入健康值");
127                     int sex=input.nextInt();
128                     System.out.println("请输入亲密度");
129                     int ban=input.nextInt();
130                     System.out.println("请输入品种");
131                     String phone=input.next();
132                     
133                     //注册添加的信息
134                     String sql="INSERT INTO dog(name,health,love,strain) VALUES(‘"+name+"‘,"+sex+","+ban+",‘"+phone+"‘);";
135                     int ro=stmt.executeUpdate(sql);
136                     if(ro>0){   //判断是否成功
137                         System.out.println("注册成功");
138                         myjdbc();
139                     }else{
140                         System.out.println("注册失败");
141                         myjdbc();
142                     }
143                 } catch (SQLException e) {
144                     // TODO Auto-generated catch block
145                     e.printStackTrace();
146                 }finally{            //关闭资源
147                     if(conn!=null){
148                         try {
149                             conn.close();
150                         } catch (SQLException e) {
151                             // TODO Auto-generated catch block
152                             e.printStackTrace();
153                         }
154                     }
155                     if(stmt!=null){
156                         try {
157                             stmt.close();
158                         } catch (SQLException e) {
159                             // TODO Auto-generated catch block
160                             e.printStackTrace();
161                         }
162                     }
163                 }
164     }
165     //二级菜单
166     private static void second() {
167         System.out.println("----------进入了2级菜单----------");
168         System.out.println("-----------1:查询所有-----------");
169         System.out.println("-----------2:修改信息-----------");
170         System.out.println("-----------3:删除-----------");
171         System.out.println("-----------4:返回1级菜单-----------");
172         System.out.println("-----------(请您选择)-----------");
173         int select=input.nextInt();
174         switch (select) {
175          case 1:  //查询所有
176              demand();
177              break;
178          case 2:  //修改信息
179              Update();
180              break;
181          case 3:   //删除
182             Drop();
183              break;
184            case 4:   //返回1级菜单
185                myjdbc() ;
186             break;
187         default:
188             System.exit(0);  //退出
189             break;
190         }
191     }
192     
193     //删除
194     private static void Drop() {
195         Connection conn=null;
196         Statement stmt=null;
197         try {
198             Class.forName(Driver);
199         } catch (ClassNotFoundException e) {
200             // TODO Auto-generated catch block
201             e.printStackTrace();
202         }
203         try {
204             conn=DriverManager.getConnection(jdbc,"epetamin","1234");
205             stmt=conn.createStatement();
206             System.out.println("请输入要删除的编号");
207             int exe=input.nextInt();
208             String delete="DELETE FROM dog WHERE id="+exe+"";
209             int ro=stmt.executeUpdate(delete);
210             if(ro>0){
211                 System.out.println("删除成功");
212                 second();
213             }else{
214                 System.out.println("删除失败");
215                 second();
216             }
217         } catch (SQLException e) {
218             // TODO Auto-generated catch block
219             e.printStackTrace();
220         }finally{
221             if(conn!=null){
222                 try {
223                     conn.close();
224                 } catch (SQLException e) {
225                     // TODO Auto-generated catch block
226                     e.printStackTrace();
227                 }
228             }
229             if(stmt!=null){
230                 try {
231                     stmt.close();
232                 } catch (SQLException e) {
233                     // TODO Auto-generated catch block
234                     e.printStackTrace();
235                 }
236             }
237         }
238     }
239     //修改信息
240     private static void Update() {
241         Connection conn=null;
242         Statement stmt=null;
243         try {
244             Class.forName(Driver);
245         } catch (ClassNotFoundException e) {
246             // TODO Auto-generated catch block
247             e.printStackTrace();
248         }
249         try {
250             conn=DriverManager.getConnection(jdbc,"epetamin","1234");
251             stmt=conn.createStatement();
252             System.out.println("请输入要修改的编号");
253             int upd=input.nextInt();
254             System.out.println("请输入要修改的名称");
255             String names=input.next();
256             String upds="update dog set name=‘"+names+"‘ where id="+upd+"";
257             int ro=stmt.executeUpdate(upds);
258             if(ro>0){
259                 System.out.println("修改成功");
260                 second();
261             }else{
262                 System.out.println("修改失败");
263                 second();
264             }
265         } catch (SQLException e) {
266             // TODO Auto-generated catch block
267             e.printStackTrace();
268         }finally{
269             if(conn!=null){
270                 try {
271                     conn.close();
272                 } catch (SQLException e) {
273                     // TODO Auto-generated catch block
274                     e.printStackTrace();
275                 }
276             }
277             if(stmt!=null){
278                 try {
279                     stmt.close();
280                 } catch (SQLException e) {
281                     // TODO Auto-generated catch block
282                     e.printStackTrace();
283                 }
284             }
285         }
286     }
287     //查询所有
288     private static void demand() {
289         Connection conn=null;
290         Statement stmt=null;
291         ResultSet ro=null;
292 
293 
294         try {
295             Class.forName(Driver);
296         } catch (ClassNotFoundException e) {
297             // TODO Auto-generated catch block
298             e.printStackTrace();
299         }
300         try {
301             conn= DriverManager.getConnection(jdbc,"epetamin","1234");
302             stmt=conn.createStatement();
303             String dem="select * from dog";
304             ro=stmt.executeQuery(dem);
305             while (ro.next()) {
306                 int studentNo = ro.getInt("id");
307                 String studentName = ro.getString("name");
308                 String loginPwd = ro.getString("strain");
309                 // 获取日期
310                 System.out.println("编号==》" + studentNo);
311                 System.out.println("姓名==》" + studentName);
312                 System.out.println("品种==》" + loginPwd);
313             }    
314             }catch (SQLException e) {
315             // TODO Auto-generated catch block
316             e.printStackTrace();
317         }finally{
318             if(conn!=null){
319                 try {
320                     conn.close();
321                 } catch (SQLException e) {
322                     // TODO Auto-generated catch block
323                     e.printStackTrace();
324                 }
325             }
326             if(stmt!=null){
327                 try {
328                     stmt.close();
329                 } catch (SQLException e) {
330                     // TODO Auto-generated catch block
331                     e.printStackTrace();
332                 }
333             }
334             if(ro!=null){
335                 try {
336                     ro.close();
337                 } catch (SQLException e) {
338                     // TODO Auto-generated catch block
339                     e.printStackTrace();
340                 }
341             }
342         }
343       }
344 }

12:16:04

jdbc链接MySQL

标签:com   value   exe   lock   case   tac   auto   else   set   

原文地址:https://www.cnblogs.com/less1/p/8716574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!