会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1817楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1818楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1820楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1821楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1822楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1824楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1825楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 1826楼

package java03192;

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

public class JdbcTest {

	/**
	 * @param 张楠楠2019年5月30日14:35
问题:
1、使用jdk中的反射机制,啥是反射机制自己都忘了。
2、Fri May 31 09:16:56 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.1
3、快捷按键
	 */
	//向Department表中添加一条数据;
	public void insertDepartment( String department_name,int location_id){
		Connection conn=null;
		Statement state=null;
		//驱动注册-》数据库驱动实例化,使用jdk中的反射机制;
		try {
			//驱动注册
			Class.forName("com.mysql.jdbc.Driver");
			//获取连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8", "root", "root");//返回connction对象
			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();//数据库中的驱动在数据库中的驱动包中=com.mysql.jdbc.Driver.class,driver.class,有一个非运行异常
		}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表中的department_id 为6的数据,将部门名称改为数学部,location_id修改为6
	public void updateDepartment(String department_name,int department_id,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="UPDATE departments d SET d.department_name='"+department_name+"',d.location_id="+location_id+" WHERE department_id="+department_id+"";
			//String sql="insert into departments values("+department_id+",'"+department_name+"',"+location_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.insertDepartment("研发部3", 10);
		test.updateDepartment("数学部",3 , 10);

	}

}
Fri May 31 10:36:35 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.lang.NullPointerException
	at java03192.JdbcTest.updateDepartment(JdbcTest.java:65)
	at java03192.JdbcTest.main(JdbcTest.java:92)

老师提示报错,查了找不到原因,请帮忙看看。

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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