java操作資料庫

來源:互聯網
上載者:User

標籤:資料庫連接   val   lex   cti   bsp   dex   設定   static   mysql配置   

參考:https://www.cnblogs.com/centor/p/6142775.html

   MySQL串連驅動:mysql-connector-java-5.1.27.jar

 

 1 package sqldemo; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8  9 public class main {10 11     public static void main(String[] args) {12         //聲明Connection對象13         Connection con;14         //驅動程式名15         String driver = "com.mysql.jdbc.Driver";16         //URL指向要訪問的資料庫名mydata17         String url = "jdbc:mysql://localhost:3306/sqltestdb";18         //MySQL配置時的使用者名稱19         String user = "root";20         //MySQL配置時的密碼21         String password = "123456";22         //遍曆查詢結果集23         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 exception64             e.printStackTrace();65         }finally{66             System.out.println("資料庫資料成功擷取!!");67         }68     }69 70 }

 

String name;String id;  PreparedStatement psql;ResultSet res;//預先處理添加資料,其中有兩個參數--“?”psql = con.prepareStatement("insert into emp (empno,ename,job,hiredate,sal) "        + "values(?,?,?,?,?)");psql.setInt(1, 3212);              //設定參數1,建立id為3212的資料psql.setString(2, "王剛");      //設定參數2,name 為王剛psql.setString(3, "總裁"); DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");Date myDate2 = dateFormat2.parse("2010-09-13");psql.setDate(4,new java.sql.Date(myDate2.getTime()));psql.setFloat(5, (float) 2000.3);psql.executeUpdate();           //執行更新
psql.close;

 

PreparedStatement psql;//預先處理更新(修改)資料,將王剛的sal改為5000.0psql = con.prepareStatement("update emp set sal = ? where ename = ?");psql.setFloat(1,(float) 5000.0);      psql.setString(2,"王剛");             psql.executeUpdate();
psql.close();

 

PreparedStatement psql;//預先處理刪除資料psql = con.prepareStatement("delete from emp where sal > ?");psql.setFloat(1, 4500);psql.executeUpdate();psql.close();

 

java操作資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.