后台数据库的增删改查 项目的
连接数据库的until帮助类
package demo1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MyDBUtil { private static String className = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://192.168.0.131:9171/sisenmess-user-database"; public static final String user = "root"; public static final String password = "123"; public static Connection getConnection() throws ClassNotFoundException, SQLException{ Class.forName(className); Connection con = DriverManager.getConnection(url,user,password); return con; } public static Statement getStatement(Connection con) throws SQLException{ Statement st = con.createStatement(); return st; } public static PreparedStatement getPreparedStatement(Connection con,String sql) throws SQLException{ PreparedStatement ps = con.prepareStatement(sql); return ps; } public static ResultSet getResultSet(Statement st,String sql) throws SQLException{ ResultSet rs = st.executeQuery(sql); /*int rs1 = st.executeUpdate(sql);*/ return rs; } public static void closeAllMethod(Connection con,Statement st,ResultSet rs){ try { if(rs!=null){ rs.close(); } if(st!=null){ st.close(); } if(con!=null){ con.close(); } } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } }
项目中的增删改查示例
package demo1; import java.util.ArrayList; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.*; public class BuguManger { public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException { BuguManger bugu= new BuguManger(); //bugu.addBugu(); //bugu.search(); //bugu.update(); bugu.deleteBugu(); } public void search() throws ClassNotFoundException, SQLException, IOException{ //查询数据 String sql = ""; Connection con1 = MyDBUtil.getConnection(); PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql); sql = "select * from userlist"; ResultSet rs1 = ps.executeQuery(sql); while(rs1.next()){ String uid=rs1.getString(1); String uid2=rs1.getString(2); System.out.println(uid); } } public void addBugu() throws ClassNotFoundException, SQLException, IOException{ //增加一条数据 String sql = ""; Connection con1 = MyDBUtil.getConnection(); PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql); InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); // System.out.print("请输入用户名:"); // uname = br1.readLine(); // System.out.print("请输入密码:"); // pword = br1.readLine(); // ps.setString(1, uname); // ps.setString(2, pword); System.out.print("请输入要添加的姓名:"); String uname = br.readLine(); // // System.out.print("请输入要添加的密码:"); String pword = br.readLine(); // // System.out.print("请输入要添加的邮箱:"); String email = br.readLine(); sql = "insert into userlist (UserId,UserNo,UserTName) values('"+uname+"','"+pword+"','"+email+"')"; int rs1 = ps.executeUpdate(sql); } //修改数据 public void update() throws ClassNotFoundException, SQLException, NumberFormatException, IOException{ InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); String sql = ""; Connection con1 = MyDBUtil.getConnection(); PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql); System.out.println("请输入你所要修改的编号:"); int id = Integer.parseInt(br.readLine()); System.out.println("请输入修改后的名字:"); String UserNo = br.readLine(); System.out.println("请输入修改后的密码:"); String UserTName = br.readLine(); sql = "update userlist set UserNo='"+UserNo+"',UserTName='"+UserTName+"'where UserId = '"+id+"'"; ps.executeUpdate(sql); System.out.println("修改成功!"); } //删除数据 public void deleteBugu() throws ClassNotFoundException, SQLException, NumberFormatException, IOException{ String sql = ""; Connection con1 = MyDBUtil.getConnection(); PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql); InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); System.out.println("请输入你所要删除的编号:"); int id = Integer.parseInt(br.readLine()); sql = "delete from userlist where userId='"+id+"'"; ps.executeUpdate(sql); } }
博主 Email:32595667@139.com QQ:32595667