文章目录
1 未预编译的增删改查
//未预编译的增删改查
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DML_DQL1test {
static void add(){
Connection con = null;//连接对象
Statement st = null;//语句对象
try{
Class.forName("com.MysqL.jdbc.Driver");//加载驱动
//连接数据库 jdbc:MysqL://<hostname>[<:3306>]/<dbname>
con = DriverManager.getConnection("jdbc:MysqL://192.168.16.241:3306/mytest", "root", "root");
st = con.createStatement();//创建语句对象
//增删改(用.executeUpdate)返回的int数据(受影响条数)--操作数据库
int i = st.executeUpdate("insert into student(sname,gender,classid) values('赵云','女','3')");
if(i>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
}catch(Exception e){
e.printstacktrace();
}finally{
try {
if (st != null)
st.close();
if (con != null)
con.close();
} catch (Exception e2) {
e2.printstacktrace();
}
}
}
static void select(){//查
Connection con = null;
Statement st = null;
ResultSet rs = null;//结果集对象
try{
Class.forName("com.MysqL.jdbc.Driver");
con = DriverManager.getConnection("jdbc:MysqL://192.168.16.241:3306/mytest", "root", "root");
st = con.createStatement();
//查(用.executeQuery)--操作数据库
rs = st.executeQuery("select * from student where sid=5");
if(rs.next()){
System.out.print("编号:"+rs.getInt("sid")+" ");
System.out.print("姓名:"+rs.getString("sname")+" ");
System.out.print("性别:"+rs.getString("gender")+" ");
System.out.print("班级:"+rs.getInt("classid")+" ");
System.out.print("分数:"+rs.getInt("score")+" ");
System.out.println("课程:"+rs.getString("cusion")+" ");
}
rs = st.executeQuery("select sname as aa from student where sid=5");
if(rs.next()){
System.out.println("姓名:"+rs.getString("aa"));
}
}catch(Exception e){
e.printstacktrace();
}finally{
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (con != null)
con.close();
} catch (Exception e2) {
e2.printstacktrace();
}
}
}
public static void main(String[] args) {
add();
select();
}
}
2 有预编译的增删改查
//预编译的增删改查
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DML_DQL2test {
static void update(){//增删改
Connection con = null;//连接对象
PreparedStatement pst = null;//预编译语句对象
try{
Class.forName("com.MysqL.jdbc.Driver");//加载驱动
//链接数据库
con = DriverManager.getConnection("jdbc:MysqL://192.168.16.241:3306/mytest", "root", "root");
//创建预编译语句对象
pst = con.prepareStatement("insert into student(sname,gender,classid) values(?,?,?)");
pst.setString(1, "小黄第");//下标从1开始
pst.setString(2,"男");
pst.setInt(3,5);
int i =pst.executeUpdate();//操作数据库 增删改
System.out.println(i>0?"成功":"失败");
}catch(Exception e){
e.printstacktrace();
}finally{
try {
if (pst != null)
pst.close();
if (con != null)
con.close();
} catch (Exception e2) {
e2.printstacktrace();
}
}
}
static void select(){//查
Connection con = null;//连接对象
PreparedStatement pst = null;//预编译语句对象
ResultSet rs = null;
try{
Class.forName("com.MysqL.jdbc.Driver");//加载驱动
//链接数据库
con = DriverManager.getConnection("jdbc:MysqL://192.168.16.241:3306/mytest", "root", "root");
//创建预编译语句对象
pst = con.prepareStatement("select * from student ");
rs = pst.executeQuery();//执行查(用.executeQuery)--操作数据库
while(rs.next()){
System.out.print("编号:"+rs.getInt("sid")+" ");
System.out.print("姓名:"+rs.getString("sname")+" ");
System.out.print("性别:"+rs.getString("gender")+" ");
System.out.print("班级:"+rs.getInt("classid")+" ");
System.out.print("分数:"+rs.getInt("score")+" ");
System.out.println("课程:"+rs.getString("cusion")+" ");
}
}catch(Exception e){
e.printstacktrace();
}finally{
try {
if (pst != null)
pst.close();
if (con != null)
con.close();
} catch (Exception e2) {
e2.printstacktrace();
}
}
}
public static void main(String[] args) {
//update();
select();
}
}
3 封装
//增删改查的封装
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class myDB{//工具类
public static final String driver="com.MysqL.jdbc.Driver";//
public static final String url="jdbc:MysqL://192.168.9.171:3306/test";
public static final String user="root";
public static final String password="root";
Connection co=null;//连接对象
PreparedStatement pst=null;//预编译语句对象
ResultSet rs=null;//结果集对象
public Connection getco(){//加载驱动和链接数据库的方法 返回的是连接对象co
try{
Class.forName(driver);//加载驱动
co=DriverManager.getConnection(url, user, password);//连接数据库
}catch(Exception e){
e.printstacktrace();
}
return co;
}
public void closeAll(){//关闭
try{
if(rs!=null) rs.close();//关闭结果集对象
if(pst!=null) pst.close();//关闭预编译语句对象
if(co!=null) co.close();//关闭连接对象
}catch(Exception e){
e.printstacktrace();
}
}
public int update(String sql,Object...obj){//增删改 返回受影响条数、 int型
int result=0;
try{
co=getco();//调用 加载驱动器和链接数据库的方法
pst=co.prepareStatement(sql);//预编译语句对象
if(obj!=null){
for(int i=0;i<obj.length;i++){
pst.setobject(i+1, obj[i]);//下标从1开始
}
}
result=pst.executeUpdate();//执行sql语句 增删改
}catch(Exception e){
e.printstacktrace();
}finally{
closeAll();//调用关闭的方法
}
return result;
}
public ResultSet select(String sql,Object...obj){//查 返回 结果集对象 rs
try{
co=getco();//调用 加载驱动器和链接数据库的方法
pst=co.prepareStatement(sql);//预编译语句对象
if(obj!=null){
for(int i=0;i<obj.length;i++){
pst.setobject(i+1, obj[i]); //下标从1开始
}
}
rs=pst.executeQuery();//执行sql语句 查
}catch(Exception e){
e.printstacktrace();
}
return rs;
}
}
import java.sql.ResultSet;
public class test {
public static void main(String[] args) {
myDB db = new myDB();
int i1=db.update("insert into student(name,gender,score) value (?,?,?)" , "小黑","女",75);
System.out.println(i1>0?"成功":"失败"); //增数据
int i2=db.update("update student set name=? where id=?", "小白",1201232065);
System.out.println(i2>0?"成功":"失败"); //改数据
int i3=db.update("delete from student where id=? or id=?", 1,2);
System.out.println(i3>0?"成功":"失败");//删数据
try {//查询数据
//调用myDB中的select方法
ResultSet rs = db.select("select * from student where id=?",1201232064);
while (rs.next()) {
System.out.println(rs.getString("name")+" "+rs.getInt("score"));
}
} catch (Exception e) {
e.printstacktrace();
}finally{
db.closeAll();//查询完 关闭
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。