基于JDBC驱动进行SQLServer数据库应用开发的基本流程和方法可参考以下博客:https://blog.csdn.net/weixin_42348522/article/details/80516401,
下面重点写利用Eclipse对数据进行操作:
四:程序清单、调试和测试结果及分析
Java全部代码如下: package test;
import java.sql.*;
public class MSS_DB { public static void main(String[] args) { PreparedStatement ps = null; Connection conn = null; ResultSet rs = null;
String url = "jdbc:sqlserver://localhost:1433;databaseName = XSCJDB"; //"jdbc:sqlserver://localhost:1433;databaseName = SPJ_TEST"是microsoft提供的java-sqlserver数据库连接驱动来访问sqlserver时的url //localhost是指你的数据库服务器地址,1433为你的sqlserver端口号! //“SPJ_TEST”是所要连接的数据库的名称 String user = "sa"; String password = "aa199102q"; try { //1.加载驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("加载驱动成功!"); }catch(Exception e) { e.printStackTrace(); System.out.println("加载驱动失败!"); } try { //2.连接 conn = DriverManager.getConnection(url,user,password);
System.out.println("连接数据库成功!");
String addString="insert into Student values(?,?,?,?,?)"; add(conn,addString);//增加函数 String id = "201716086";
delete(conn,id);//删除函数
String ID = "201716086"; String name = "陈大强"; String Ssex="男"; int Sage=19; String Sdept="MA";
update(conn,ID,name,Ssex,Sage,Sdept);//修改函数
String ID1 = "201716081"; selectAVG( conn, ID1 );
String C = "数学"; selectOrderByGrade(conn,C);
// Statement s=ct.createStatement();
}catch(Exception e) { e.printStackTrace(); System.out.println("连接数据库失败!"); } }
private static void add(Connection conn, String addString) throws SQLException { String id = "201716087"; String name = "林强"; String Ssex="男"; int Sage=19; String Sdept="MA";
PreparedStatement ps = conn.prepareStatement(addString); ps.setString(1, id); ps.setString(2, name); ps.setString(3, Ssex); ps.setInt(4, Sage); ps.setString(5,Sdept);
ps.executeUpdate(); System.out.println("添加成功");
}
public static void delete(Connection conn, String id) throws SQLException { String deleteString = "delete from Student where Sno=?"; PreparedStatement ps = conn.prepareStatement(deleteString); ps.setString(1, id); System.out.println(ps.executeUpdate()); System.out.println("删除成功"); }
public static void update(Connection conn,String ID,String name,String Ssex,int Sage,String Sdept) throws SQLException { String updateString = "update Student set Sname=? where Sno=?"; PreparedStatement ps = conn.prepareStatement(updateString); ps.setString(1, ID); ps.setString(2, name);
ps.executeUpdate(); System.out.println("修改成功"); }
public static void selectAVG(Connection conn, String id) throws SQLException { String s = "select AVG(SC.Grade) from Student s join SC sc on s.Sno=sc.Sno where s.Sno=?"; PreparedStatement ps = conn.prepareStatement(s); ps.setString(1, id); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("平均成绩是:" + rs.getInt(1)); } }
public static void selectOrderByGrade(Connection conn, String cName) throws SQLException { String s = "select s.Sname from Student s join SC sc on s.Sno=sc.Sno join Course c on sc.Cno=c.Cno where c.Cname= ? order by sc.Grade"; PreparedStatement ps = conn.prepareStatement(s); ps.setString(1, cName); ResultSet rs = ps.executeQuery(); System.out.println(cName + "分数从小到大的学生姓名为:"); while (rs.next()) { System.out.println(rs.getString(1)); } }
数据库的内容:
}
private static void add(Connection conn, String addString) throws SQLException { String id = "201716087"; String name = "韩磊"; String Ssex="男"; int Sage=19; String Sdept="MA";
PreparedStatement ps = conn.prepareStatement(addString); ps.setString(1, id); ps.setString(2, name); ps.setString(3, Ssex); ps.setInt(4, Sage); ps.setString(5,Sdept);
ps.executeUpdate(); System.out.println("添加成功");
}
public static void delete(Connection conn, String id) throws SQLException { String deleteString = "delete from Student where Sno=?"; PreparedStatement ps = conn.prepareStatement(deleteString); ps.setString(1, id); System.out.println(ps.executeUpdate()); System.out.println("删除成功"); }
public static void update(Connection conn,String ID,String name,String Ssex,int Sage,String Sdept) throws SQLException { String updateString = "update Student set Sname=? where Sno=?"; PreparedStatement ps = conn.prepareStatement(updateString); ps.setString(1, ID); ps.setString(2, name);
ps.executeUpdate(); System.out.println("修改成功"); }
public static void selectAVG(Connection conn, String id) throws SQLException { String s = "select AVG(SC.Grade) from Student s join SC sc on s.Sno=sc.Sno where s.Sno=?"; PreparedStatement ps = conn.prepareStatement(s); ps.setString(1, id); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("平均成绩是:" + rs.getInt(1)); } }
public static void selectOrderByGrade(Connection conn, String cName) throws SQLException { String s = "select s.Sname from Student s join SC sc on s.Sno=sc.Sno join Course c on sc.Cno=c.Cno where c.Cname= ? order by sc.Grade"; PreparedStatement ps = conn.prepareStatement(s); ps.setString(1, cName); ResultSet rs = ps.executeQuery(); System.out.println(cName + "分数从小到大的学生姓名为:"); while (rs.next()) { System.out.println(rs.getString(1)); }
|