JavaJDBC技术:(三)通过Statement向表中插⼊数据与修改数据通过 Statement 向表中插⼊数据与修改数据
1.下载数据库驱动
1.MySQL 驱动
2.Oracle 驱动
数据库安装⽬录\oracle\product\11.2.0\dbhome_1\jdbc\lib
2.创建项⽬添加驱动
创建⼀个正常的项⽬我相信我不⽤说了吧… ⼿动滑稽
添加jar包的话我之前有说链接在下⾯
3.通过 Statement 向表中插⼊数据
public class Text{
public static void main(String[] args)throws SQLException, ClassNotFoundException {
Text t=new Text();
t.insertFuser("aaa","aaaa");
}
public void insertFuser(String name,String pwd)throws ClassNotFoundException, SQLException {
java的jdbc连接数据库//注册驱动
Class.forName("sql.jdbc.Driver");
/
/获取链接属性含义链接url,⽤户名,密码
Connection conn = Connection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8","root","Root");        String sql="insert into fuser values(default,'"+name+"','"+pwd+"')";
//执⾏SQL
Statement state = ateStatement();
if(state != null){
try{
state.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
运⾏结果:
4.通过 Statement 对象修改表中的数据
将上⼀步插⼊的数据a就该为b
public class Text{
public static void main(String[] args)throws SQLException, ClassNotFoundException {
Text t=new Text();
t.updateFuser(2,"bbb","bbb");
}
public void updateFuser(int id,String name,String pwd)throws ClassNotFoundException, SQLException {
Class.forName("sql.jdbc.Driver");
Connection conn = Connection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8","root","Root");        String sql="update fuser  set uname = '"+name+"',upassword = '"+pwd+"' where id ="+id;
Statement state = ateStatement();
if(state != null){
try{
state.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
运⾏结果: