步骤详解:
设计并实现数据库在数据库中插入相应的数据链接SQLServer数据库实现对数据库的查增删等功能数据库内容效果图
目录结构 1 . 数据库链接(在zshang包里) 1.1 DBHelper.java代码
package com.zshang; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.*; public class DBHelper { private static String driver; private static String url; private static String name; private static String pwd; private static Connection con; //初始化,获取四个变量的值 private static void init(){ Properties p=new Properties(); try{ p.load(DBHelper.class.getResourceAsStream("info.properties")); driver=p.getProperty("driver"); url=p.getProperty("url"); name=p.getProperty("name"); pwd=p.getProperty("pwd"); }catch(IOException e){ e.printStackTrace(); } } //获取连接 public static Connection getCon(){ init(); try{ Class.forName(driver); try{ con=DriverManager.getConnection(url,name,pwd); }catch(SQLException e){ e.printStackTrace(); } }catch(ClassNotFoundException e){ e.printStackTrace(); } return con; } }1.2 info.properties代码
2 . 对数据库数据进行相关操作
2.1 查询操作代码及效果图
代码展示
package com.zsh; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.zshang.DBHelper; public class STest { public static void main(String[] args) throws SQLException { Connection con=null; PreparedStatement ps=null; ResultSet rs=null; String sql="select * from people"; con=DBHelper.getCon(); try{ ps=con.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ System.out.print(rs.getInt(1)); System.out.print(":"); System.out.println(rs.getString(2)); } }catch(SQLException e){ e.printStackTrace(); }finally{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(con!=null){ con.close(); } } } }效果图 2.2 删除操作代码及效果图 代码展示
package com.zsh; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.zshang.DBHelper; public class DTest { public static void main(String[] args) throws SQLException { Connection con=null; PreparedStatement ps=null; String sql = "delete from people where id = 2"; con=DBHelper.getCon(); try{ ps=con.prepareStatement(sql); int i=ps.executeUpdate(); if (i > 0) { System.out.println("删除数据成功"); } else { System.out.println("删除数据失败"); } }catch(SQLException e){ e.printStackTrace(); }finally{ if(ps!=null){ ps.close(); } if(con!=null){ con.close(); } } } }效果图 2.3 增加操作的代码及效果图 代码展示
package com.zsh; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.zshang.DBHelper; public class ITest { public static void main(String[] args) throws SQLException { Connection con=null; PreparedStatement ps=null; String sql = "insert into people (id,name) values(1,'孙悟空')"; con=DBHelper.getCon(); try{ ps=con.prepareStatement(sql); int i=ps.executeUpdate(); if (i > 0) { System.out.println("插入数据成功"); } else { System.out.println("插入数据失败"); } }catch(SQLException e){ e.printStackTrace(); }finally{ if(ps!=null){ ps.close(); } if(con!=null){ con.close(); } } } }效果图 相信上述代码会对你的JAVA之旅有很大的帮助,了解更多关注我呦!!!