会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132661个问题
Python 全系列/第七阶段:Web全栈开发基础与前端/JavaScript语言 1771楼
JAVA 全系列/第十二阶段:消息中间件(异步消息传递)/ActiveMQ 1773楼
JAVA 全系列/第十二阶段:Spring Cloud Alibaba技术栈/Zookeeper 1774楼

问题一:在sql语句中,为什么int值也需要使用“”拼接?

问题二:反射后并没被使用,无意义啊,不创建反射程序依然进行

代码:

package cn.bjsxt.jdbc;

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","123456");
			String sql="insert into departments values(default,'"+department_name+"',"+location_id+")" ;
			state = conn.createStatement();
			int flag = state.executeUpdate(sql);
			System.out.println(flag);
		} 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();
				}
			}
		}
	}
	public void updateDepartments(int department_id,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","123456");
			String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id = "+location_id+" where d.department_id ="+department_id;
			
			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 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(5, "市场部", 5);
	}

}

运行结果:

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1778楼
JAVA 全系列/第九阶段:权限控制与安全认证/Shiro(旧) 1780楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 1781楼

XML 解析错误:格式不佳 位置:http://localhost:8080/jnData.do 行 1,列 1:




    Title
    
    
        () {
        =().();
        =().();
        .({
            :,
            :,
            :.({
                :,
                :}),
            :(sesult) {
                ().(result);
            }
        });
        }
    



:

()




Selvlet;

com.JsonUtils;

javax.servlet.ServletException;
javax.servlet.annotation.;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
java.io.IOException;
java.io.PrintWriter;
java.util.HashMap;
java.util.Map;

()
JsonDataServlet HttpServlet {
    doGet(HttpServletRequest req, HttpServletResponse resp) ServletException, IOException {
        String userid=req.getParameter();
        String username=req.getParameter();
        Map<String,String> map=HashMap<>();
        map.put(,userid);
        map.put(,username);
        String s= JsonUtils.(map);
        resp.setContentType();
        PrintWriter pw=resp.getWriter();
        pw.print(s);
        pw.flush();
        pw.close();
    }

    doPost(HttpServletRequest req, HttpServletResponse resp) ServletException, IOException {
        String s=req.getReader().readLine();
        PrintWriter pw=resp.getWriter();
        pw.print(s);
        pw.flush();
        pw.close();
    }
}


JAVA 全系列/第五阶段:JavaWeb开发/Ajax技术详解(旧) 1782楼
Python 全系列/第一阶段:Python入门/面向对象 1783楼
人工智能/第十二阶段:深度学习-图像识别原理/卷积神经网络优化 1784楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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