加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java连接mysql数据库示例

发布时间:2020-05-24 21:34:06 所属栏目:Java 来源:互联网
导读:java连接mysql数据库示例

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

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

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读