使用Statement操作数据库

时间:2021-04-13 19:38:50

i

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

public class TestDemo {
 public static final String DBDRIVER="net.sourceforge.jtds.jdbc.Driver";
 public static final String DBURL
  ="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=pengmengyue";
 public static final String DBUSER="sa";
 public static final String PASSWORD="123";
  
 public static void main(String[] args) throws Exception {
  Connection conn=null;
  Statement stmt=null;
  Class.forName(DBDRIVER);
  conn=DriverManager.getConnection(DBURL);
  stmt=conn.createStatement();
  String sql="SELECT sno,cno,Grade from Grade";
  ResultSet rs=stmt.executeQuery(sql);
  while(rs.next()){
  String sno=rs.getString("sno");
  String cno=rs.getString("cno");
  int Grade=rs.getInt("Grade");
  System.out.println("学号:"+sno+"课程号:"+cno+"成绩:"+Grade);
 }
  rs.close();
  stmt.close(); 
  conn.close();
 }

}

使用Statement操作数据库