java mysql 数据库
生活随笔
收集整理的這篇文章主要介紹了
java mysql 数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. jdbc 驅動名還是數據庫
String driver = "com.mysql.jdbc.Driver";//URL指向要訪問的數據庫名mydataString url = "jdbc:mysql://localhost:3306/sqltestdb"
2.
數據庫名 String user = "root";//MySQL配置時的密碼
String password = "123456";
3. try { 24 //加載驅動程序 25 Class.forName(driver); 26 //1.getConnection()方法,連接MySQL數據庫!! 27 con = DriverManager.getConnection(url,user,password); 28 if(!con.isClosed()) 29 System.out.println("Succeeded connecting to the Database!"); 30 //2.創建statement類對象,用來執行SQL語句!! 31 Statement statement = con.createStatement(); 32 //要執行的SQL語句 33 String sql = "select * from emp"; 34 //3.ResultSet類,用來存放獲取的結果集!! 35 ResultSet rs = statement.executeQuery(sql); 36 System.out.println("-----------------"); 37 System.out.println("執行結果如下所示:"); 38 System.out.println("-----------------"); 39 System.out.println("姓名" + "\t" + "職稱"); 40 System.out.println("-----------------"); 41 42 String job = null; 43 String id = null; 44 while(rs.next()){ 45 //獲取stuname這列數據 46 job = rs.getString("job"); 47 //獲取stuid這列數據 48 id = rs.getString("ename"); 49 50 //輸出結果 51 System.out.println(id + "\t" + job); 52 } 53 rs.close(); 54 con.close(); 55 } catch(ClassNotFoundException e) { 56 //數據庫驅動類異常處理 57 System.out.println("Sorry,can`t find the Driver!"); 58 e.printStackTrace(); 59 } catch(SQLException e) { 60 //數據庫連接失敗異常處理 61 e.printStackTrace(); 62 }catch (Exception e) { 63 // TODO: handle exception 64 e.printStackTrace(); 65 }finally{ 66 System.out.println("數據庫數據成功獲取!!"); 67 } 68 } 69 70 }
轉載于:https://www.cnblogs.com/guangzhou11/p/8659809.html
總結
以上是生活随笔為你收集整理的java mysql 数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CASIO 5800P计算器游戏--猜数
- 下一篇: linux cmake编译源码,linu