mariadb的jdbc

 

 


package com.dasenlin.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *测试jdbc 
 */
public class Test {
    private static final String url="jdbc:mysql://192.168.0.180/test";
    private static final String user="yaya";
    private static final String password="123qqq...A";
    private static final String driverName="org.mariadb.jdbc.Driver";
    public static void main(String[] args) {
        Connection conn=null;
        Statement state=null;
        ResultSet result=null;
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, user, password);
            state =conn.createStatement();
            //insert(state);
            //update(state);
            delete(state);
            result =state.executeQuery("select * from shop;");
            while(result.next()){
                int id = result.getInt(1);
                String name = result.getString(2);
                System.out.println("id:"+id+"\tname:"+name);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                if(result!=null)result.close();
                if(state!=null)state.close();
                if(conn!=null)conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
    }
    /**
     *添加 
     * @throws SQLException 
     */
    public static void insert(Statement state) throws SQLException{
        String sql = "insert into shop (id ,name) values(4,'汽车');";
        state.execute(sql);
    }
    /**
     *修改 
     * @throws SQLException 
     */
    public static void update(Statement state) throws SQLException{
        String sql = "update shop set name='airplan' where id=4;";
        int result = state.executeUpdate(sql);
        if(result>0){
            System.out.println("更新成功");
        }
    }
    /**
     *删除 
     * @throws SQLException 
     */
    public static void delete(Statement state) throws SQLException{
        String sql ="delete from shop where id=4;";
        state.execute(sql);
    }
}


package com.dasenlin.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *测试jdbc 
 */
public class Test {
    private static final String url="jdbc:mysql://192.168.0.180/test";
    private static final String user="yaya";
    private static final String password="123qqq...A";
    private static final String driverName="org.mariadb.jdbc.Driver";
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet result=null;
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, user, password);
            //insert(conn);
            //update(conn);
            delete(conn);
            ps=conn.prepareStatement("select * from shop");
            result =ps.executeQuery();
            while(result.next()){
                int id = result.getInt(1);
                String name = result.getString(2);
                System.out.println("id:"+id+"\tname:"+name);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                if(result!=null)result.close();
                if(ps!=null)ps.close();
                if(conn!=null)conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
    }
    /**
     *添加 
     * @throws SQLException 
     */
    public static void insert(Connection conn) throws SQLException{
        String sql = "insert into shop (id ,name) values(?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1, 5);
        ps.setString(2, "汽车");
        ps.executeUpdate();
    }
    /**
     *修改 
     * @throws SQLException 
     */
    public static void update(Connection conn) throws SQLException{
        String sql = "update shop set name=? where id=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, "sir");
        ps.setInt(2, 4);
        int result = ps.executeUpdate();
        if(result>0){
            System.out.println("更新成功");
        }
    }
    /**
     *删除 
     * @throws SQLException 
     */
    public static void delete(Connection conn) throws SQLException{
        String sql ="delete from shop where id=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1, 4);
        ps.executeUpdate();
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值