Java最简单的JDBCODBC数据库操作

//-----------------------------数据库插入操作------------------------------------------
import java.sql.*;

public class SqlConnection {

       public static void main(String[] args)
      {  
             PreparedStatement prestate = null;
             try
             {
                  int iRow = 0;
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  Connection conn = DriverManager.getConnection("jdbc:odbc:ODBCDataSource","sa","sa");
                  prestate = conn.prepareStatement("Insert T_Trial Values(?,?)");
    
                  prestate.setString(1,"100001");
                  prestate.setString(2, "zhangps");
      
                  iRow=prestate.executeUpdate();
      
                  if(iRow>0)
                  {
                       System.out.println("Update Success");
                  }
                  else
                  {
                       System.out.println("Update Fails");
                  }
       
                  conn.close(); 
               }
                catch(Exception ex)
                {
                     ex.printStackTrace();
                }
      }
}
//-----------------------------数据库查询操作------------------------------------------

import java.sql.*;

public class SqlConnection {

     public static void main(String[] args)
     {  
          ResultSet result = null;
          Statement stmt = null;
          try
          {
               
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection conn = DriverManager.getConnection("jdbc:odbc:JavaPaper","sa","sa");
                stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                result = stmt.executeQuery("Select * From BackAppend Where Bka_NO < 7");

                while(result.next())
                {
                     System.out.print(result.getString("Bka_NO"));
                     System.out.print(result.getString("Bka_Name"));
                     System.out.println(result.getString("Bka_Time"));
                  }

                 conn.close();
            }
            catch(Exception ex)
            {
                  ex.printStackTrace();
             }
        }
}

你可能感兴趣的:(java,sql,jdbc,sun)