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

你可能感兴趣的:(JAVA OOP 第八章JDBC练习+Basedao)