JAVA OOP 第八章JDBC练习+Basedao

来源:互联网 发布:vscode 编码方式默认 编辑:程序博客网 时间:2024/05/18 21:47
package ls.happy1;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class Test1select {public static void main(String[] args) throws Exception {// TODO Auto-generated method stub String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";  String url="jdbc:sqlserver://localhost:1433;DatabaseName=subway";  String username="sa";  String pwd="1";  Class.forName(driver);    Connection con=DriverManager.getConnection(url, username, pwd);  String sql="select * from subwayinfo";  Statement state=con.createStatement();       ResultSet rs=state.executeQuery(sql);while(rs.next()){String subwayname=rs.getString("subwayname");System.out.println(subwayname);}rs.close();state.close();con.close();}}

package ls.happy1;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class Test {public static void main(String[] args) throws Exception{// TODO Auto-generated method stub//  String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";  String url="jdbc:sqlserver://192.168.60.124:1433;DatabaseName=MySchool";  String username="sa";  String pwd="6375196";  Class.forName(driver);  Connection con=DriverManager.getConnection(url, username, pwd);  String sql="insert into Work values('Smith',6000,'excellerate your efforts.')";  Statement state=con.createStatement(); int count= state.executeUpdate(sql); if(count>0){ System.out.println("添加成功!!"); }  state.close();  con.close();  }}

package cn.happy2.com;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import java.util.concurrent.Executors;public class JDBCupdate {public static void main(String[] args) throws Exception{// TODO Auto-generated method stub String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";  String url="jdbc:sqlserver://localhost:1433;DatabaseName=MySchool";  String username="sa";  String pwd="1";  Class.forName(driver);  Connection con=DriverManager.getConnection(url, username, pwd);  String sql="update IsLogin set pwd='1'where id=3";  Statement state=con.createStatement(); int count= state.executeUpdate(sql); if(count>0){ System.out.println("修改成功!!"); }  state.close();  con.close();  }}

package cn.happy3.com;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class JDBCdelete {public static void main(String[] args) throws Exception{// TODO Auto-generated method stub String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";  String url="jdbc:sqlserver://localhost:1433;DatabaseName=MySchool";  String username="sa";  String pwd="1";  Class.forName(driver);  Connection con=DriverManager.getConnection(url, username, pwd);  String sql="delete from IsLogin where id=3";  Statement state=con.createStatement(); int count= state.executeUpdate(sql); if(count>0){ System.out.println("删除成功!!"); }  state.close();  con.close();  }}

package ls.happy.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Basedao {public static final  String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";public static final  String url="jdbc:sqlserver://localhost:1433;DatabaseName=subway";public static final  String username="sa";public static final  String pwd="1";  private Connection con;private PreparedStatement ps;private ResultSet rs;//执行增删改public int executeUpdate(String sql,Object...objs) throws Exception{ps=con.prepareStatement(sql);for(int i=0;i<objs.length;i++){ps.setObject(i+1,objs[i]);}int count=ps.executeUpdate(); return count;}//读取操作public ResultSet executeQuery(String sql,Object...objs) throws Exception{ps=con.prepareStatement(sql);for(int i=0;i<objs.length;i++){ps.setObject(i+1,objs[i]);}rs=ps.executeQuery(); return rs;}//关闭连接public void closeResoercers(){}static{try{Class.forName(driver);}catch(Exception e){e.printStackTrace();}}public Connection getconnection() throws Exception{try{if(con==null||con.isClosed())con=DriverManager.getConnection(url, username, pwd);}catch(Exception e){e.printStackTrace();}return con;}}

0 0