Java 利用 JDBC 连接 Sqlsever2012 实现 增删改查

所需驱动文件下载

http://download.csdn.net/detail/u012320991/9378730

数据表如下:

Java 利用 JDBC 连接 Sqlsever2012 实现 增删改查_第1张图片

运行结果如下:

Java 利用 JDBC 连接 Sqlsever2012 实现 增删改查_第2张图片


解决过程(默认SqlSever  已安装并配置好):

下载  JDBC 4.0 For SqlSever ---> 添加到项目库------>导入公共帮助类SqlHelper.class【提前写好】----->编写测试代码


相应源代码如下:

公共帮助类SqlHelper.class

package cn.xyj;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlHelper
{

	
	private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";

	private static String dbURL="jdbc:sqlserver://52xyj.cn:1433;DatabaseName=NoteBook";

	private static String userName="sa";

	private static String userPwd="***";
	  
	  
	private static Connection  getCoonection()
	  {
		  try
		  {
		   Class.forName(driverName);
		   Connection conn=DriverManager.getConnection(dbURL,userName,userPwd);
		   return conn;
		  }

		  catch(Exception e)
		  {
		   e.printStackTrace();
		   System.out.print("----------------连接失败");
		  } 
		  return null;
	  }
	  
	public static ResultSet  executeQuery(String SQL)
	{  
		  try

		  {

		   Connection conn=getCoonection();
		   System.out.println("---------------连接数据库成功");  
		  // String SQL = "SELECT PlanTypeID, PlanTypeName FROM C_PlanType ";
		   Statement stmt = conn.createStatement();
		   ResultSet rs = stmt.executeQuery(SQL);
		     /* while (rs.next()) 
		      {
		         System.out.println(rs.getString("PlanTypeID") + ", " + rs.getString("PlanTypeName"));
		      }*/
		     // rs.close();
		     // stmt.close(); 
		      return  rs;
		  }
		  catch(Exception e)
		  {
		   e.printStackTrace();
		   System.out.print("----------------查询失败");
		  }
		  return null;
	}
	public static boolean  executeUpdate(String SQL)
	{  
		  try
		  {
		   Connection conn=getCoonection();
		   System.out.println("---------------连接数据库成功");  
		  
		   Statement stmt = conn.createStatement();
		   int result = stmt.executeUpdate(SQL);
		   if(result>0)
			   return true;
		  }
		  catch(Exception e)
		  {
		   e.printStackTrace();
		   System.out.print("----------------更新失败");
		  }
		  return false;
	}
}

测试代码


package cn.xyj;


import java.sql.ResultSet;
import java.sql.SQLException;

public class Hello 
{

	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException 
	{
		testQuery();//查询
		 testDelete();//删除
		testQuery();//删除后查询
		  testInsert();//插入
		testQuery();//插入后查询
		testUpdate();//更新
		testQuery();//更新后查询	     
	}
	
	private static void testQuery() throws SQLException
	{
		String SQL = "SELECT PlanTypeID, PlanTypeName FROM C_PlanType ";
		ResultSet rs=SqlHelper.executeQuery(SQL);
		 while (rs.next()) 
		 {
	         System.out.println(rs.getString("PlanTypeID") + ", " + rs.getString("PlanTypeName"));
	      }
	}
	private static void testInsert()
	{
		 String SQL = "  insert into C_PlanType(PlanTypeID,PlanTypeName,DisplayOrder)values('7','测试','7') ";
		 if(SqlHelper.executeUpdate(SQL))
		 {
			 System.out.println("插入成功 ");
		 }else
		 {
			 System.out.println("插入失败 ");
		 }
	}
	private static void testUpdate()
	{
		 String  SQL = "  update  C_PlanType set PlanTypeName='测试修改'  where PlanTypeID='7'";
		 if(SqlHelper.executeUpdate(SQL))
		 {
			 System.out.println("更新成功 ");
		 }else
		 {
			 System.out.println("更新失败 ");
		 }
	}
	private static void testDelete()
	{
		String SQL = "  delete from  C_PlanType   where PlanTypeID='7'";
		 if(SqlHelper.executeUpdate(SQL))
		 {
			 System.out.println("删除成功 ");
		 }else
		 {
			 System.out.println("删除失败 ");
		 }
	}

}




你可能感兴趣的:(数据库,JAVA)