问题一:在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);
}
}
运行结果:
