会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132384个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 36856楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/MyCat 36857楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 36860楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 36861楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Dubbo 36862楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-使用VSFTPD_Nginx完成商品新增 36864楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 36867楼

问题点:为什么我用JAVA查询出的数据是相同的而实际在MySQL查询的值是如下图所示

JdbcTest

package cn.bjsxt.jdbc5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.bjsxt.jdbc2.jdbcUtil;

public class JdbcTest {
	public void insertDempartments(String departmentName,int locationId){
	Connection conn = null;
	PreparedStatement pr = null;
	try{
		conn = JdbcUtil.getConnection();
		pr = conn.prepareStatement("insert into departments values(default,?,?)");
		pr.setString(1,departmentName);
		pr.setInt(2, locationId);
		pr.execute();
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		jdbcUtil.closeResource(pr, conn);
	}
	}
	public void updateDempartments(int department_id,String department_name,int location){
		Connection conn = null;
		PreparedStatement pr = null;
		try{
			conn = JdbcUtil.getConnection();
			pr = conn.prepareStatement("update departments set department_name = ?,location_id = ? where department_id = ?");
			pr.setString(1, department_name);
			pr.setInt(2,location);
			pr.setInt(3, department_id);
			pr.execute();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public Departments selectDepartment(int departmentId){
		Connection conn = null;
		PreparedStatement pr = null;
		ResultSet re = null;
		Departments dept = null;
		try{
			conn = JdbcUtil.getConnection();
			pr = conn.prepareStatement("select * from departments where department_id = ?");
			pr.setInt(1, departmentId);
			re=pr.executeQuery();
			while(re.next()){
			dept = new Departments();
			dept.setDepartmentId(re.getInt("department_id"));
			dept.setLocation(re.getInt("location_id"));
			dept.setDepartmentName(re.getString("department_name"));
			}
			}catch(Exception e){
			e.printStackTrace();
			
			
		}finally{
			JdbcUtil.closeResorce(conn, re, pr);
		}
		return dept;
	}
	public List<Departments> selectDepartmentByLikeName(String departmentName){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Departments> list = new ArrayList<>();
		try{
			conn = JdbcUtil.getConnection(); 
			ps = conn.prepareStatement("select * from departments where department_name like ?");
			ps.setString(1, "%"+departmentName+"%");
			rs = ps.executeQuery();
			while(rs.next()){
				Departments dept = new Departments();
				 dept.setDepartmentId(rs.getInt("department_id"));
				 dept.setDepartmentName(rs.getString("department_name"));
				 dept.setLocation(rs.getInt("location_id"));
				 list.add(dept);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.closeResorce(conn, rs, ps);
		}
		return list;
	}
	
	public static void main(String [] args){
		JdbcTest jdbc = new JdbcTest();
		//jdbc.insertDempartments("人力资源部3",10);
		//jdbc.updateDempartments(8, "人力资源部1", 10);
		/*Departments dept = jdbc.selectDepartment(8);
		if(dept!=null){
			System.out.println(dept.getDepartmentId()+" "+dept.getDepartmentName()+" "+dept.getLocation());
		}*/
		List<Departments> list = jdbc.selectDepartmentByLikeName("人力");
		for(Departments d:list){
			System.out.println(d.getDepartmentId()+" "+d.getDepartmentName()+" "+d.getLocation());
		}
	
	}

}

JdbcUtil:

package cn.bjsxt.jdbc5;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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{
		//读取Properties文件
		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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获取Connection对象
	public static Connection getConnection(){
		Connection conn = null;
		
		try{
		conn = DriverManager.getConnection(jdbcUrl,username,userpassword);	
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	//关闭Statement
	public static void closeStatiemt(Statement state){
		if(state!=null){
			try {
				state.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//关闭Connection
	public static void closeConnection(Connection conn){
		if(conn!=null){
			try{
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	//关闭ResultSet
	public static void closeResultSet(ResultSet re){
		if(re!=null){
			try {
				re.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//关闭资源
	public static void closeResorce(Connection conn,ResultSet re,Statement state){
		JdbcUtil.closeConnection(conn);
		JdbcUtil.closeResultSet(re);
		JdbcUtil.closeStatiemt(state);
		
		
	}
	
}

jdbc.properties:

driver =com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8
username =root
userpassword=123456

Java查询:

image.png

MySQL查询:

image.png

包:

jdbcDemo.rar


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

为什么我一点登录就提示下下载,打开下载的html文件是响应结果

1557993934661669.png

源码如下:

package cn.sxt.server;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * B/S编程:服务器端
 * 基于TCP协议
 * @author User
 *
 */
public class ServerHtml {
 public static void main(String[] args) {
  //换行
  String CRLF="\r\n";
  //空格
  String BLANK=" ";
  //1.创建ServerSocket对象
  ServerSocket ss=null;
  //2.使用字节流
  InputStream is=null;
  
  try {
   ss = new ServerSocket(8888);
   //2.监听客户端,调用accept方法,得到Socket对象
   Socket client=ss.accept();
   /**
    * 获取来自浏览器的请求信息
    */
   //使用字节流流读取浏览器请求信息
   is=client.getInputStream();
   //创建缓存数组
   byte[] b=new byte[1<<13];
   //将信息读到数组
   int len=is.read(b);
   //打印信息,使用String类的构造函数来讲数组信息打印出来
   System.out.println(new String(b,0,len));
   /**
    * 对web浏览器的请求作出响应
    * 分三步:
    * 1.HTTP协议、版本号、状态代码、描述
    * 2.响应头
    * 3.响应正文
    */
   StringBuilder sb=new StringBuilder();
   StringBuilder sbContent=new StringBuilder();//响应的正文
   sbContent.append("<html><head><title>响应结果</title></head>");
   sbContent.append("<body>登录成功</body></html>");
   //拼接响应头:HTTP协议、版本号、状态代码、描述
   sb.append("HTTP/1.1").append(BLANK).append(200).append(BLANK).append("OK");
   //响应头内容:
   sb.append(CRLF);//换行
   sb.append("Content-Type: baiduApp/json; v6.27.2.14; charset=UTF-8");
   sb.append(CRLF);//换行
   sb.append("Content-length:").append(sbContent.toString().getBytes().length).append(CRLF);
   sb.append(CRLF);//换行
   sb.append(sbContent);//追加正文
   
   //通过流,输出到浏览器     //转化流
   BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));
   bw.write(sb.toString());
   bw.flush();
   bw.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
  //5.关闭流
  IOClose.close(is,ss);
  }
 }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 36869楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Dubbo 36870楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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