美文网首页
Java操作MySQL数据库(增删改查)

Java操作MySQL数据库(增删改查)

作者: 嫩牛_软件测试_笔记 | 来源:发表于2018-09-27 15:29 被阅读0次

package DbConn;
import java.sql.*;

public class MysqlUpdate3 {
    public static void main(String[] args) {
        try{
            String driver="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/51Testing";
            String usr="root";
            String pwd="";
            Class.forName(driver);
            Connection conn=DriverManager.getConnection(url,usr,pwd);
            if(!conn.isClosed()){
                System.out.println("数据库连接成功!");
            }
            Statement st=conn.createStatement();

            String sql="drop table test1a  ";
            int i=st.executeUpdate(sql);
            if(i==0){
                System.out.println("成功删除了表!");
            }

            /*String sql="create table test1a(id int(4),name varchar(20))";
            int i=st.executeUpdate(sql);
            if(i==0){
                System.out.println("成功创建了表!");
            }*/


            /*String sql="delete from classmate where id=200 or id=16 ";
            int i=st.executeUpdate(sql);
            if(i>0){
                System.out.println("成功删除了"+i+"条记录");
            }
            */
            /*String sql="update classmate set name='LinQX' where id=200 ";
            int i=st.executeUpdate(sql);
            if(i>0){
                System.out.println("成功修改了"+i+"条记录");
            }*/
            /*String sql="insert into classmate(id,name,age) values(200,\'林青霞\',50)";
            int i=st.executeUpdate(sql);
            if(i>0){
                System.out.println("成功插入了"+i+"条记录");
            }*/
            st.close();
            conn.close();
        }catch (ClassNotFoundException e){
            System.out.println("驱动加载失败!");
            e.printStackTrace();
        }catch (SQLException e){
            System.out.println("SQL语句执行失败!");
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

相关文章

网友评论

      本文标题:Java操作MySQL数据库(增删改查)

      本文链接:https://www.haomeiwen.com/subject/ycbeoftx.html