JAVA OOP 第八章JDBC练习+Basedao

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;
		}
		}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值