JDBC连接并简单操作数据库存(自己学习用)

 //本例子是用sun公司自带的jdbc包,通过ODBC连数据源;以及用微软的包来连,注意下面是两套不一样的连接方法,都可以用。
import java.io.*;
import java.sql.*;
public class testJdbc
{
 public static void main(String [] args)
 {
  try
   {
   //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//装载驱动,这个是sun的
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//这个是微软的
   }
  catch(ClassNotFoundException ce)
   {
    System.out.println(ce);
   }
  try
   {
    //String url="jdbc:odbc:jyxjyx";  //指定ODBC连接的名称,这个是Sun的
                //这个是微软的,注意DatabaseName后面是数据库名,注意上面sun公司的url中用的ODBC的数据源名,不是数据库名。
    InputStreamReader in =new InputStreamReader(System.in);//System.in是字节流,通过InputStreamReader转化为字符流
    BufferedReader keyboard =new BufferedReader(in);//因为要使用BufferedReader的readLine的方法,而BufferedReader的参数必须是字符流
    System.out.println("输入员工姓名:");
    String EmpName=keyboard.readLine();
       String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jyx";
    String user ="sa";
    String password="";
    //String intable="employee";
    String inname=EmpName;
    
    Connection con=DriverManager.getConnection(url,user,password);//连接数据库,注意getConnection的参数
    //Statement s=con.createStatement();//产生运行SQL语句的对象
    //PreparedStatement pstmt=con.prepareStatement("select * from employee where (name=? or ? is null)");
    //PreparedStatement pstmt=con.prepareStatement("select * from employee where name like ?");
    //PreparedStatement pstmt=con.prepareStatement("update employee set name=? where name like ?");
    PreparedStatement pstmt=con.prepareStatement("update employee set name=? where name in (?,?)");
    //pstmt.setString(1,intable);
    //pstmt.setString(1,"employee");
    pstmt.setString(1,inname);
    pstmt.setString(2,"六军");
    pstmt.setString(3,"赵二村");
    pstmt.executeUpdate();  //executeUpdate()用于更新操作
    //String sql="select * from "+intable+" where name = '"+inname+"'";//自己凑一个sql
    //ResultSet rs=s.executeQuery(sql);//返回结果集
    //ResultSet rs=pstmt.executeQuery();//executeQuery()用于查询返回结果集
    /*while(rs.next())
     {
     System.out.print(rs.getInt("Gid")+"/t");
     System.out.print("||");
     System.out.print(rs.getString("name")+"/t");
     System.out.print("||");
     System.out.print(rs.getString("IDCARD")+"/t");
     System.out.print("||");
     System.out.print(rs.getDate("CREATEDATE")+"/t");
     System.out.println();
     }*/
    
    //s.executeUpdate("insert into employee(GID,CODE,NAME) values(1000030,'hsc','何三村')");
    //s.executeUpdate("update employee set name='赵二村' where name='何二村'");
    //s.executeUpdate("delete from employee where name='何三村'");
   }
  catch(Exception e)
   {
   e.printStackTrace();
   }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值