当前位置:Gxlcms > 数据库问题 > java连接mysql数据库样例

java连接mysql数据库样例

时间:2021-07-01 10:21:17 帮助过:2人阅读

  1. package packagename;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class classname {
  10.     public static String url = "jdbc:mysql://localhost:3306/test";//characterEncoding=GBK
  11.     public static String username = "root";
  12.     public static String password = "root";
  13.     public static Connection con;
  14.     public static Statement stmt;
  15.     public static ResultSet rs;
  16.     public static PreparedStatement pstmt;
  17.     
  18.     public static void main(String[] args) throws SQLException {
  19.         connect();
  20.         //select();
  21.         //insert();
  22.         //update();
  23.         //delete();
  24.         close();
  25.     }
  26.     public static void connect() {
  27.         // 定位驱动
  28.         try {
  29.             Class.forName("com.mysql.jdbc.Driver");
  30.             System.out.println("加载驱动成功!"); 
  31.         } catch (ClassNotFoundException e) {
  32.             System.out.println("加载驱动失败!");
  33.             e.printStackTrace();
  34.         }
  35.         // 建立连接
  36.         try {
  37.             con = DriverManager.getConnection(url, username, password);
  38.             stmt = con.createStatement();
  39.             System.out.println("数据库连接成功!"); 
  40.         } catch(SQLException e) {
  41.             System.out.println("数据库连接失败!");
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.     public static void select() {
  46.         try {
  47.          
  48.          String sql="select * from test where name=? "; 
  49.             pstmt=con.prepareStatement(sql); 
  50.            pstmt.setString(1,"root"); 
  51.          //String sql="select * from test where name=‘root‘ "; 
  52.             //rs = stmt.executeQuery(sql);
  53.            rs=pstmt.executeQuery();
  54.             while (rs.next()) {
  55.              System.out.println("你的第一个字段内容为:"+rs.getString("name")); 
  56.              System.out.println("你的第二个字段内容为:"+rs.getInt(1)); 
  57.             }
  58.             rs.close();
  59.         }catch (Exception e) {
  60.             System.out.println("数据查询失败!");
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.     public static void insert() {
  65.         try {
  66.          String sql="insert into test (id,name) values(‘2‘,‘admin‘)";
  67.          stmt.executeUpdate(sql);
  68.             System.out.println("数据插入成功!");
  69.         }catch (Exception e) {
  70.             System.out.println("数据插入失败!");
  71.             e.printStackTrace();
  72.         }
  73.         
  74.     }
  75.     public static void update() {
  76.         try {
  77.          String sql="update test set name=‘rootroot‘ where id=1";
  78.             stmt.executeUpdate(sql);
  79.             System.out.println("数据更新成功!");
  80.         }catch (Exception e) {
  81.             System.out.println("数据更新失败!");
  82.             e.printStackTrace();
  83.         }
  84.     }
  85.     public static void delete() {
  86.         try {
  87.          String sql="delete from test where id=?";
  88.           pstmt = con.prepareStatement(sql);
  89.              pstmt.setInt(1,1);
  90.              pstmt.executeUpdate();
  91.             System.out.println("数据删除成功!");
  92.         }catch (Exception e) {
  93.             System.out.println("数据删除失败!");
  94.             e.printStackTrace();
  95.         }
  96.     }
  97.     public static void close() {
  98.      try{
  99.       if(rs!=null)
  100.              rs.close();
  101.       if(stmt!=null)
  102.              stmt.close();
  103.       if(con!=null)
  104.              con.close();
  105.      }catch(Exception e)
  106.      {
  107.       e.printStackTrace();
  108.      }
  109.     }
  110.     
  111. }

java连接mysql数据库样例

标签:

人气教程排行