您的当前位置:首页正文

import java

2023-12-12 来源:客趣旅游网
import java.sql.*; public class JDBCTest {

public static void main(String args[])throws SQLException {

String createString1; String createString2; String createString3; Statement stmt;

createString1=\"create table student\"+

\"(sno char(7) not null unique,\"+ \"sname char(15) not null,\"+ \"sssex char(2),\"+ \"sage int,\"+ \"sdept char(5))\"; createString2=\"create table course\"+

\"(cno char(4) not null unique,\"+ \"cname char(20) ,\"+ \"cpno char(4),\"+ \"ccredit int)\";

createString3=\"create table sc\"+

\"(sno char(7) not null ,\"+ \"cno char(4) not null,\"+ \"grade int)\"; try {

Class.forName(\"com.kingbase.Driver\"); }catch(java.lang.ClassNotFoundException e) {

System.err.print(\"classNotFoundException:\");

System.err.println(e.getMessage()); } try{

Connection con; String ur1=\"JDBC\";

con=DriverManager.getConnection(ur1,\"SYSTEM\",\"MANAGER\"); stmt=con.createStatement(); stmt.execute(createString1); stmt.execute(createString2); stmt.execute(createString3); con.setAutoCommit(false);

stmt.addBatch(\"insert into student\"+\"values('95001','李勇','男

','20','cs')\");

stmt.addBatch(\"insert into student\"+\"values('95002','刘晨','女','19','is')\");

stmt.addBatch(\"insert into student\"+\"values('95003','王敏','女','18','ma')\");

stmt.addBatch(\"insert into student\"+\"values('95004','张立','男','19','is')\");

stmt.addBatch(\"insert into course\"+\"values('1','数据库','5','4')\");

stmt.addBatch(\"insert into course\"+\"values('2','数学','2','2')\");

stmt.addBatch(\"insert into course\"+\"values('3','信息系统','1','4')\");

stmt.addBatch(\"insert into course\"+\"values('4','操作系统','6','3')\");

stmt.addBatch(\"insert into sc\"+\"values('95001','1','92')\"); stmt.addBatch(\"insert into sc\"+\"values('95002','2','90')\"); stmt.addBatch(\"insert into sc\"+\"values('95003','6','78')\"); stmt.addBatch(\"insert into sc\"+\"values('95004','5','80')\"); int[] updateCounts= stmt.executeBatch(); con.commit();

con.setAutoCommit(true);

ResultSet uprs=stmt.executeQuery(\"SELECT*FROM Student\"); System.out.println(\"Table Student after insertion:\"); System.out.println(\"sno sname ssex sage sdept\"); while(uprs.next()){ }

System.out.println(\"\");

uprs=stmt.executeQuery(\"select*from course\");

System.out.println(\"Table Course after insertion:\"); System.out.println(\"cno cname cpno ccredit\"); while(uprs.next()){ }

String cno=uprs.getString(\"cno\"); String name=uprs.getString(\"cname\"); String pno=uprs.getString(\"cpno\"); int credit=uprs.getInt(\"ccredit\");

System.out.println(cno+\"\"+name+\"\"+pno+\"\"+credit); String sno=uprs.getString(\"sno\"); String name=uprs.getString(\"sname\"); String sex=uprs.getString(\"ssex\"); int age=uprs.getInt(\"sqage\");

String dept=uprs.getString(\"sdept\");

System.out.println(sno+\"\"+name+\"\"+sex+\"\"+age+\"\"+dept);

System.out.println(\"\");

uprs=stmt.executeQuery(\"select*from sc\");

System.out.println(\"Table SC after insertion:\"); con.setAutoCommit(true);

ResultSet uprs=stmt.executeQuery(\"SELECT*FROM Student\"); System.out.println(\"Table Student after insertion:\"); System.out.println(\"sno sname ssex sage sdept\"); while(uprs.next()){ }

System.out.println(\"\");

uprs=stmt.executeQuery(\"select*from course\");

System.out.println(\"Table Course after insertion:\"); System.out.println(\"cno cname cpno ccredit\"); while(uprs.next()){ }

System.out.println(\"\");

uprs=stmt.executeQuery(\"select*from sc\");

System.out.println(\"Table SC after insertion:\"); System.out.println(\"SNO CNO GRADE\"); while(uprs.next()){ }

System.out.println(\"\"); uprs.close(); stmt.close(); con.close(); }

catch(BatchUpdateException b){

System.err.println(\"-----BatchUpdateExcept-----\"); String sno=uprs.getString(\"SNO\"); String cno=uprs.getString(\"CNO\"); String grade=uprs.getString(\"GRADE\"); System.out.println(sno+\"\"+cno+\"\"+grade); String cno=uprs.getString(\"cno\"); String name=uprs.getString(\"cname\"); String pno=uprs.getString(\"cpno\"); int credit=uprs.getInt(\"ccredit\");

System.out.println(cno+\"\"+name+\"\"+pno+\"\"+credit); String sno=uprs.getString(\"sno\"); String name=uprs.getString(\"sname\"); String sex=uprs.getString(\"ssex\"); int age=uprs.getInt(\"sqage\");

String dept=uprs.getString(\"sdept\");

System.out.println(sno+\"\"+name+\"\"+sex+\"\"+age+\"\"+dept);

con.commit();

}

System.err.println(\"SQLSTate: \"+b.getSQLState()); System.err.println(\"Message: \"+b.getMessage()); System.err.println(\"Vendor: \"+b.getErrorCode()); System.err .println(\"Update count: \"); int[] updateCounts=b.getUpdateCounts(); for (int i=0;iSystem.err.println(\"\"); }catch(SQLException ex){ }

System.err.print(\"----SQLException----\");

System.err.println(\"SQLState: \"+ex.getSQLState()); System.err.println(\"Message: \"+ex.getMessage()); System.err.println(\"Vendor: \"+ex.getErrorCode()); System.err.print(updateCounts[i]+\" \");

}

因篇幅问题不能全部显示,请点此查看更多更全内容