会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132651个问题

问题在哪呀

package com.llz.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class JdbcUtil {
    private static String driver;
    private static String JdbcUrl;
    private static String userName;
    private static String userPassword ;
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("Jdbc");
        driver = bundle.getString("driver");
        JdbcUrl = bundle.getString("JdbcUrl");
        userName = bundle.getString("userName");
        userPassword = bundle.getString("userPassword");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取connection对象
    public static Connection getConnection() {
        Connection conn = null;

        try {
            conn = DriverManager.getConnection(JdbcUrl, userName, userPassword);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }
    //关闭
    public static void closeState(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
    public static void closeConn(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

}
package com.llz.jdbc;


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

class JdbcTest{
    public void test(int id,String address,int amount){
        Connection conn = null;
        Statement state = null;
        try{
            conn = JdbcUtil.getConnection();
            String sql = "insert into goodcount values('"+id+"','"+address+"','"+amount+"')";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtil.closeState(state);
            JdbcUtil.closeConn(conn);
        }
    }
    public void updateTest(int id,String address,int amount){
        Connection conn = null;
        Statement state = null;
        try{
            conn=JdbcUtil.getConnection();
            String sql = "update goodcount set id = '"+id+"',address='"+address+"' where amount="+amount+"";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtil.closeConn(conn);
            JdbcUtil.closeState(state);
        }
    }
}
public class StatementTest {
    public static void main(String[] args) {

        JdbcTest jt = new JdbcTest();
        jt.test(6,"重庆",5);
        //jt.updateTest(8,"湖南",5);
    }
}
driver = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://localhost:3306/good?useUnicode=true&characterEncoding=utf-8
userName = root
userPassword =123456


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 860楼

老师这是为什么

image.png

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

class JdbcTest{
    public void test(int id,String address,int amount){
        Connection conn = null;
        Statement state = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/good?useUnicode=true&characterEncoding=utf-8";
            conn = DriverManager.getConnection(url,"root","123456");
            String sql = "insert into goodcount values('"+id+"','"+address+"','"+amount+"')";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(state!=null){
                    state.close();
                }
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    public void updateTest(int id,String address,String amount){
        Connection conn = null;
        Statement state = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/good?useUnicode=true&characterEncoding=utf-8";
            conn = DriverManager.getConnection(url,"root","123456");
            String sql = "update goodcount set id = '"+id+"',address='"+address+"' where amount='"+amount+"'";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(state!=null){
                    state.close();
                }
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}
public class StatementTest {
    public static void main(String[] args) {

        JdbcTest jt = new JdbcTest();
        //jt.test(5,"长沙",5);
        jt.updateTest(5,"湖南","8");
    }

image.pngimage.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 862楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 864楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 866楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 867楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 868楼

SQL> select last_name,job_id from employees where ( job_id='SA_REP' or job_id='AD_PRES' ) and salary>1500;

LAST_NAME                 JOB_ID

------------------------- ----------

King                      AD_PRES

Tucker                    SA_REP

Bernstein                 SA_REP

Hall                      SA_REP

Olsen                     SA_REP

Cambrault                 SA_REP

Tuvault                   SA_REP

King                      SA_REP

Sully                     SA_REP

McEwen                    SA_REP

Smith                     SA_REP

Doran                     SA_REP

Sewall                    SA_REP

Vishney                   SA_REP

Greene                    SA_REP

Marvins                   SA_REP

Lee                       SA_REP

Ande                      SA_REP

Banda                     SA_REP

Ozer                      SA_REP

LAST_NAME                 JOB_ID

------------------------- ----------

Bloom                     SA_REP

Fox                       SA_REP

Smith                     SA_REP

Bates                     SA_REP

Kumar                     SA_REP

Abel                      SA_REP

Hutton                    SA_REP

Taylor                    SA_REP

Livingston                SA_REP

Grant                     SA_REP

Johnson                   SA_REP

31 rows selected


SQL> select last_name, job_id from employees where (job_id='SA_REP' or job_id='AD_PRES') and salary>15000;

LAST_NAME                 JOB_ID

------------------------- ----------

King                      AD_PRES

老师我这两个命令一样,为什么输出的不一样

JAVA 全系列/第三阶段:数据库编程/SQL 语言 870楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637