Eclipse+Java数据库实现“增删改查”代码

建立包:

shixun

导入jar(这里我用的是D:\Java\jdk-15.0.2\lib\mssql-jdbc-9.2.1.jre11.jar)

想要下载的在这下载

链接:https://pan.baidu.com/s/1xp3VHBcFHqhqV2iRDxFUEg 
提取码:zyg5

具体导入:

1.打开一个项目,右击

找到 Build Path->configure Build path

2.

Eclipse+Java数据库实现“增删改查”代码_第1张图片

 3.

Eclipse+Java数据库实现“增删改查”代码_第2张图片

 4.导入jar

Eclipse+Java数据库实现“增删改查”代码_第3张图片

 5.应用

Eclipse+Java数据库实现“增删改查”代码_第4张图片

 ok,

下面,我们上代码

插入代码

AddJdbc.java

package shixun.day02;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import javax.swing.JOptionPane;

public class AddJdbc {
	
		public static void main(String[] args) {
			//声明JDBC驱动程序对象
			String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
		     //定义JDBC的URL对象,TestDB为数据源
			String conURL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
			String userName="sa";
			String userPwd="123";
			try {
			      Class.forName(JDriver);
			    }
			   catch(java.lang.ClassNotFoundException e) {
			      System.out.println("ForName :" + e.getMessage( ));
			    }
			   try {
			      Connection con=DriverManager.getConnection(conURL,userName,userPwd);
			      Statement s=con.createStatement( );      
			      String r1="insert into stu(name,id,age,sex) values ('张三',1,18,'男')";
			      String r2="insert into stu(name,id,age,sex) values ('李四',2,19,'男')";
			      String r3="insert into stu(name,id,age,sex) values ('赵五',3,18,'男')";
			      //使用SQL 命令insert插入三条学生记录到表中
			      s.executeUpdate(r1);
			      s.executeUpdate(r2);
			      s.executeUpdate(r3);
			      s.close( );
			      con.close( );
			    }
			   catch(SQLException e) 
			    {
			      System.out.println("SQLException: " +e.getMessage( ));
			    }
			  }
	}

更新代码

UpdateDate.java

package shixun.day02;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import javax.swing.JOptionPane;

public class UpdateDate {
	
		public static void main(String[] args) {
			//声明JDBC驱动程序对象
			String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
		     //定义JDBC的URL对象,TestDB为数据源
			String conURL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
			String userName="sa";
			String userPwd="123";
			try {
			      Class.forName(JDriver);
			    }
			   catch(java.lang.ClassNotFoundException e) {
			      System.out.println("ForName :" + e.getMessage( ));
			    }
			   try {
			      Connection con=DriverManager.getConnection(conURL,userName,userPwd);
			      Statement s=con.createStatement( );      
			      String r1="UPDATE stu set name='lib'  where id=1";
			      更新数据
			      s.executeUpdate(r1);
			    
			      s.close( );
			      con.close( );
			    }
			   catch(SQLException e) 
			    {
			      System.out.println("SQLException: " +e.getMessage( ));
			    }
			  }
	}

删除代码

Delete.java

package shixun.day02;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import javax.swing.JOptionPane;

public class Delete {
	
		public static void main(String[] args) {
			//声明JDBC驱动程序对象
			String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
		     //定义JDBC的URL对象,TestDB为数据源
			String conURL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
			String userName="sa";
			String userPwd="123";
			try {
			      Class.forName(JDriver);
			    }
			   catch(java.lang.ClassNotFoundException e) {
			      System.out.println("ForName :" + e.getMessage( ));
			    }
			   try {
			      Connection con=DriverManager.getConnection(conURL,userName,userPwd);
			      Statement s=con.createStatement( );      
			      String r1="delete from stu where id = 2";
			      System.out.println("删除成功! ");
			      //删除
			      s.executeUpdate(r1);
			    
			      s.close( );
			      con.close( );
			    }
			   catch(SQLException e) 
			    {
			      System.out.println("SQLException:! " +e.getMessage( ));
			    }
			  }
	}

查找代码

Select.java

package shixun.day02;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import javax.swing.JOptionPane;

public class Select {
	
		public static void main(String[] args) {
			//声明JDBC驱动程序对象
			String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
		     //定义JDBC的URL对象,TestDB为数据源
			String conURL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
			String userName="sa";
			String userPwd="123";
			try {
			      Class.forName(JDriver);
			    }
			   catch(java.lang.ClassNotFoundException e) {
			      System.out.println("ForName :" + e.getMessage( ));
			    }
			   try {
			      Connection con=DriverManager.getConnection(conURL,userName,userPwd);
			      Statement s=con.createStatement( );      
			      String r1="select * from stu where id = 2";
			      //查询代码
			      s.executeUpdate(r1);
			    
			      s.close( );
			     
			    
			   Statement m = con.createStatement();
				ResultSet rs = m.executeQuery("select * from student");
				while (rs.next()) {
					System.out.println(rs.getString("name") + "\t" + rs.getInt("id") + "\t" + rs.getInt("age")+ "\t" + rs.getString("sex"));
				}
				m.close();
				con.close();
			} catch (SQLException e) {
				System.out.println("SQLException: " + e.getMessage());
			}
		}
	
			  }
	

你可能感兴趣的:(Java,java,big,data,eclipse)