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)
老师提示报错,查了找不到原因,请帮忙看看。