java連接mysql數據庫示例

mww8 9年前發布 | 4K 次閱讀 Java

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();
 }
}

}</pre>

 本文由用戶 mww8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!