java串連mysql資料庫範例

來源:互聯網
上載者:User

標籤:

package packagename;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement; public class classname {    public static String url = "jdbc:mysql://localhost:3306/test";//characterEncoding=GBK    public static String username = "root";    public static String password = "root";    public static Connection con;    public static Statement stmt;    public static ResultSet rs;    public static PreparedStatement pstmt;        public static void main(String[] args) throws SQLException {        connect();        //select();        //insert();        //update();        //delete();        close();    }    public static void connect() {        // 定位驅動        try {            Class.forName("com.mysql.jdbc.Driver");            System.out.println("載入驅動成功!");         } catch (ClassNotFoundException e) {            System.out.println("載入驅動失敗!");            e.printStackTrace();        }        // 建立串連        try {            con = DriverManager.getConnection(url, username, password);            stmt = con.createStatement();            System.out.println("資料庫連接成功!");         } catch(SQLException e) {            System.out.println("資料庫連接失敗!");            e.printStackTrace();        }    }    public static void select() {        try {                  String sql="select * from test where name=? ";             pstmt=con.prepareStatement(sql);            pstmt.setString(1,"root");          //String sql="select * from test where name=‘root‘ ";             //rs = stmt.executeQuery(sql);           rs=pstmt.executeQuery();            while (rs.next()) {             System.out.println("你的第一個欄位內容為:"+rs.getString("name"));              System.out.println("你的第二個欄位內容為:"+rs.getInt(1));             }            rs.close();        }catch (Exception e) {            System.out.println("資料查詢失敗!");            e.printStackTrace();        }    }    public static void insert() {        try {         String sql="insert into test (id,name) values(‘2‘,‘admin‘)";         stmt.executeUpdate(sql);            System.out.println("資料插入成功!");        }catch (Exception e) {            System.out.println("資料插入失敗!");            e.printStackTrace();        }            }    public static void update() {        try {         String sql="update test set name=‘rootroot‘ where id=1";            stmt.executeUpdate(sql);            System.out.println("資料更新成功!");        }catch (Exception e) {            System.out.println("資料更新失敗!");            e.printStackTrace();        }    }    public static void delete() {        try {         String sql="delete from test where id=?";          pstmt = con.prepareStatement(sql);             pstmt.setInt(1,1);             pstmt.executeUpdate();            System.out.println("資料刪除成功!");        }catch (Exception e) {            System.out.println("資料刪除失敗!");            e.printStackTrace();        }    }    public static void close() {     try{      if(rs!=null)             rs.close();      if(stmt!=null)             stmt.close();      if(con!=null)             con.close();     }catch(Exception e)     {      e.printStackTrace();     }    }    }

java串連mysql資料庫範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.