TXT

SQLjava

By Ernest Woods,2014-10-06 16:53
12 views 0
SQLjava

查询

import java.sql.*;

    public class q1{

     public static void main(String args[]){

     try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}

     catch(ClassNotFoundException e1){

     System.out.print("加载驱动程序失败!");

     System.out.print(e1.getMessage());}

     Connection conn=null;

     Statement stmt=null;

     try {conn=DriverManager.getConnection("jdbc:odbc:student");

     stmt = conn.createStatement();

     ResultSet rs=stmt.executeQuery("select * from student");

     rs.next();

     do{

     String xm=rs.getString("xm");

     String xb=rs.getString("xb");

     String csrq=rs.getString("csrq");

     String jtzz=rs.getString("jtzz");

     String dhhm=rs.getString("dhhm");

     System.out.print(xm+" "+xb+" "+csrq+" "+jtzz+" "+dhhm);

     System.out.println(" ");

     }while(rs.next());

     }

     catch (SQLException er){

     System.out.println("查询失败!");}

     }

     }

添加

import java.sql.*;

    public class q2{

     public static void main(String args[]){

     try {

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

     }catch (ClassNotFoundException ee) {

     System.out.println ("加载驱动程序失败;");

     return; }

     try {

     Connection

conn=DriverManager.getConnection("jdbc:odbc:student1","","");

     Statement Stmt=conn.createStatement();

     Stmt.executeUpdate("insert into student1 values('08010911','单东净','',#1989-02-28#,'020102','15963301336','我是一只小鸭子呀咿呀咿呀哟!')");

     System.out.println("插入新记录成功;");

     conn.close();

     }catch(SQLException e1) {

     System.out.println ("插入失败;");

     return;

     }

     }

     }

删除

import java.sql.*;

    public class q3{

     public static void main(String args[]){

     try {

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

     }catch (ClassNotFoundException ee) {

     System.out.println ("加载驱动程序失败;");

     return; }

     try {

     Connection

    conn=DriverManager.getConnection("jdbc:odbc:student1","","");

     Statement Stmt=conn.createStatement();

     String sqlstr="delete from student1 where name='单东净'";

     System.out.println("删除记录成功;");

     Stmt.executeUpdate(sqlstr);

     conn.close();

     }catch(SQLException e1) {

     System.out.println ("删除记录失败;");

     return;

     }

     }

     }

Report this document

For any questions or suggestions please email
cust-service@docsford.com