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

老师,我的这个代码可以成功插入数据,可是在执行成功前会显示异常是怎么回事?

public class JdbcUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    
    static {
        ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        url=bundle.getString("url");
        username=bundle.getString("username");
        password=bundle.getString("password");
        
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    private static void closeStatement(Statement state){
        if(state != null){
            try {
                state.close();
            } catch (Exception e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void closeConnection(Connection conn){
        if(conn != null){
            try {
                conn.close();
            } catch (Exception e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void closeResource(Statement state,Connection conn){
        closeStatement(state);
        closeConnection(conn);
    }


}
public class JDBCTest {
    public void insertData(String department_name,int location_id){
        Connection conn=null;
        Statement state=null;
        try {
            conn=JdbcUtil.getConnection();
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/zf?useUnicode=true&characterEncoding=utf8","root","zfroot");
            String sql="insert into department value(default ,'"+department_name+"',"+location_id+")";
            state=conn.createStatement();
            int flag=state.executeUpdate(sql);
            System.out.println(flag);
            } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.closeResource(state,conn);
        }
    }

    public void updateData(String department_name,int location_id,int department_id){
        Connection conn=null;
        Statement state=null;
        try {
            conn=JdbcUtil.getConnection();
            String sql="update department set department_name='"+department_name+"',location_id="+location_id+" where department_id="+department_id+";";
            state=conn.createStatement();
            int flag=state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.closeResource(state,conn);

        }
    }

    public static void main(String[] args) {
        JDBCTest jdbcTest=new JDBCTest();
//        jdbcTest.updateData("策划部",6,3);
        jdbcTest.insertData("经理部",7);
    }
}

image.png

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 931楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 932楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 935楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 937楼

package com.bjsxt;

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

public class JdbcTest {
    Connection conn = null;
    Statement state = null;

    //向Departments表中添加一条数据
    public void insertDepartments(String department_name,int location_id){
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=UTF-8","root","root");
            //执行SQL
            //如果数据以参数形式传入,需进行字符串拼接
            String sql = "insert into departments values(default,'"+department_name+"','"+location_id+"')";
            //String sql = "insert into departments values(default,'研发部',8)";
            //
            state = conn.createStatement();
             int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
    //更新departments表中的department_id为4的数据,将部门名称改为教学部,location_id修改为6
    public void updateDepartments(String department_name,int location_id,int department_id){
        Connection conn = null;
        Statement state = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=UTF-8","root","root");
            state = conn.createStatement();
            String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id ="+location_id+"where d.department_id = "+department_id;
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    public static void main (String[] args){
        JdbcTest test = new JdbcTest();
        //test.insertDepartments("研发部",8);
        test.updateDepartments("教学部",6,11);
    }
}

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 938楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 939楼

package com.bjsxt;

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

public class JdbcTest {
	//向Departments 表中添加一条数据
	public void insertDepartments(String department_name,int location_id){
		Connection  conn = null;
		Statement state = null;
		try {
			//驱动注册
			Class.forName("com.mysql.jdbc.Driver");
			//创建连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root","root");
			String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
			state = conn.createStatement();
			int flat = state.executeUpdate(sql);
			System.out.println(flat);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(state != null){
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}if(conn!= null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
	//更新Departments
	public void updateDepartments(String department_name,int location_id,int department_id){
		Connection conn =null;
		Statement state =null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8","root","root");
			state=conn.createStatement();
			String sql="update departments d set d.department_name='"+department_name+"',d.location_id = "+location_id+ "where d.department_id = "+department_id;
			
			int flag= state.executeUpdate(sql);
			System.out.println(flag);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(state != null){
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}if(conn!= null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}
	public static void main(String[] args) {
		JdbcTest test=new JdbcTest();
		//test.insertDepartments("研发部", 8);
		test.updateDepartments("教学部", 6, 6);
	}
	

}

报错

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'd.department_id = 6' at line 1
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
	at com.mysql.jdbc.Util.getInstance(Util.java:384)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2690)
	at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1648)
	at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1567)
	at com.bjsxt.JdbcTest.updateDepartments(JdbcTest.java:54)
	at com.bjsxt.JdbcTest.main(JdbcTest.java:81)

老师,这是哪里出了问题啊,找了很久也没找着原因



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

package com.cx;

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

/**
 * 使用封装工具
 */
public class jdbcTest2 {
    //向Department表中添加一条数据
    public void insertDeparments(String department_name,int location_id){
        Connection conn = null;
        Statement state = null;
        try {
            //使用封装工具
            conn = jdbcUtil1.getConnection();

            String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
            state = conn.createStatement();   //通过conn对象调用Connection接口下createStatement方法
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            jdbcUtil1.closeConnection(conn);
            jdbcUtil1.closeStatment(state);
        }
    }
    //更新departments表中的department_id为6的数据,将部门名称修改为教学不,location_id修改为6
    public  void  updateDepartments(String department_name,int location_id,int department_id){
        Connection conn = null;
        Statement state = null;
        try {
            conn= jdbcUtil1.getConnection();
            state = conn.createStatement();
            String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id ="+location_id+" where d.department_id ="+department_id+"";
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbcUtil1.closeConnection(conn);
            jdbcUtil1.closeStatment(state);
        }
    }
    //查询Departments表中部门ID为6的部门信息
    public void selectDepartmentsById(int departmentId){
        Connection conn = null;
        Statement state = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtil1.getConnection();
            state = conn.createStatement();
            String sql = "select * from departments d where d.department_id = "+departmentId;
            //执行查询返回结果
            rs = state.executeQuery(sql);
            while (rs.next()){
                int a = rs.getInt("department_id");
                String s = rs.getString("department_name");
                int b = rs.getInt(3);
                System.out.println(a+" "+s+" "+b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbcUtil1.closeConnection(conn);
            jdbcUtil1.closeStatment(state);
            jdbcUtil1.closeResultSet(rs);
        }
    }

    public static void main(String[] args) {
        jdbcTest2 test2 = new jdbcTest2();
        //test.insertDeparments("工程部",6);
       // test.updateDepartments("研发",6,8);
        test2.selectDepartmentsById(6);
    }
   
}
package com.cx;


import java.sql.*;
import java.util.ResourceBundle;

/**
 * 封装jdbc工具类
 */
public class jdbcUtil1 {
    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 (Exception e){
            e.printStackTrace();
        }
    }
    //获取Connection对象
    public static Connection getConnection() {
        Connection conn = null;
        try {
            //获取连接
            conn = DriverManager.getConnection(jdbcUrl,username,userpassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //关闭Statement
    public static void closeStatment(Statement state){
        try {
            if (state != null){
                state.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //关闭Connection
    public static void closeConnection(Connection conn){
        try {
            if (conn != null){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //关闭
    public  static void closeResultSet(ResultSet rs){
        try {
            if (rs != null){
                rs.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
driver="com.mysql.jdbc.Driver"
jdbcUrl ="jdbc:mysql://localhost:3306/caixx?useUnicode=true&usecharacterEncoding=utf-8&useSSL=false"
username="root"
userpassword="root"


发现使用properties就会报错

image.png


不使用properties就正常, IDEA到底如何使用这个?

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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