实验1 验证连接
一、实验目的
掌握java连接数据库的操作
二、实验内容
创建一个测试类,连接student数据库,测试连接数据库是否成功,并显示学生信息表中数据。
三、程序代码
1. import java.sql.Connection; 2. import java.sql.DriverManager; 3. import java.sql.ResultSet; 4. import java.sql.SQLException; 5. import java.sql.Statement; 6. 7. public class Connect { 8. public static void main(String[] args) throws SQLException,ClassNotFoundException { 9. Connection conn = null; 10. try { 11. Class.forName("com.mysql.jdbc.Driver"); 12. System.out.println("驱动加载成功"); 13. }catch (ClassNotFoundException e) { 14. e.printStackTrace(); 15. System.out.println("驱动加载失败"); 16. } 17. String url="jdbc:mysql://localhost:3306/student"; 18. String user="root"; 19. String password="123"; 20. try { 21. conn = DriverManager.getConnection(url,user,password); 22. System.out.println("数据库连接成功"); 23. //创建命令对象 24. Statement stmt = conn.createStatement(); 25. //给出sql语句,查询test表中的所有数据 26. String sql = "select * from stuinfo"; 27. //执行sql语句,得到多条记录,结果集ResultSet 28. ResultSet rs = stmt.executeQuery(sql); 29. while(rs.next()) { 30. //getString(columIndex) columIndex表示列号,从1开始 31. System.out.println("学号:"+rs.getInt(1)); 32. System.out.println("姓名:"+rs.getString(2)); 33. System.out.println("性别:"+rs.getString(3)); 34. System.out.println("班级:"+rs.getString(4)); 35. System.out.println("电话:"+rs.getInt(5)); 36. } 37. rs.close(); 38. stmt.close(); 39. conn.close(); 40. } catch (SQLException e) { 41. // TODO Auto-generated catch block 42. e.printStackTrace(); 43. System.out.println("连接失败"); 44. } 45. } 46. }
四、实验指导
1、掌握连接数据库的步骤
(1)注册JDBC Driver
(2)获得与数据库的物理连接
(3)创建不同类型的Statement
(4)执行SQL命令
(5)如果有结果集, 处理结果集
(6)释放资源
实验2 数据库操作
一、实验目的
掌握DriverManager、Connection、Statement、PreparedStatement、ResultSet五大类的使用方法
二、实验内容
设计一个实现学生信息管理的程序,要求合法的学生才能进入系统,在系统中可以查看学生的基本信息和成绩,并能对各个信息进行简单的增删改操作。
三、程序代码
1. import java.sql.Connection; 2. import java.sql.DriverManager; 3. import java.sql.ResultSet; 4. import java.sql.SQLException; 5. import java.sql.Statement; 6. import java.util.Scanner; 7. 8. import com.mysql.jdbc.PreparedStatement; 9. 10. public class Manage { 11. public static void main(String[] args) throws SQLException,ClassNotFoundException { 12. Connection conn = null; 13. try { 14. Class.forName("com.mysql.jdbc.Driver"); 15. System.out.println("驱动加载成功"); 16. }catch (ClassNotFoundException e) { 17. e.printStackTrace(); 18. System.out.println("驱动加载失败"); 19. } 20. String url="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=gbk"; 21. String user="root"; 22. String password="123"; 23. try { 24. conn = DriverManager.getConnection(url,user,password); 25. System.out.println("数据库连接成功"); 26. //创建命令对象 27. Statement stmt = conn.createStatement(); 28. System.out.println("请输入选择:1--查看 2--增加 3--删除 4--修改"); 29. Scanner val = new Scanner(System.in); 30. int op = val.nextInt(); 31. switch(op) { 32. case 1: 33. //给出sql语句,查询test表中的所有数据 34. String sql1 = "select * from score"; 35. //执行sql语句,得到多条记录,结果集ResultSet 36. ResultSet rs = stmt.executeQuery(sql1); 37. while(rs.next()) { 38. //getString(columIndex) columIndex表示列号,从1开始 39. System.out.println("学号:"+rs.getInt(1)); 40. System.out.println("课程名:"+rs.getString(2)); 41. System.out.println("分数:"+rs.getInt(3)); 42. } 43. rs.close(); 44. stmt.close(); 45. conn.close(); 46. break; 47. case 2: 48. Statement stmt2 = conn.createStatement(); 49. int adsno = 0; 50. String adcname = null; 51. int adscore = 0; 52. System.out.println("请输入要增加的内容:"); 53. adsno = val.nextInt(); 54. adcname = val.next(); 55. adscore = val.nextInt(); 56. String sql2 = "insert into score (sno,cname,score) value("+adsno+",'"+adcname+"',"+adscore+")"; 57. int result2 = stmt.executeUpdate(sql2); 58. System.out.println("增加成功"); 59. break; 60. case 3: 61. int sno = val.nextInt(); 62. System.out.println("请输入要删除的学号:"); 63. String sql3 = "delete from score where sno = "+sno; 64. int result3 = stmt.executeUpdate(sql3); 65. System.out.println("删除成功"); 66. break; 67. case 4: 68. Statement stmt4 = conn.createStatement(); 69. int upsno = 0; 70. String upcname = null; 71. int upscore = 0; 72. System.out.println("请输入要修改的学号和内容(如:1 化学 99):"); 73. upsno = val.nextInt(); 74. upcname = val.next(); 75. upscore = val.nextInt(); 76. String sql4 = "update score set sno = ? where cname = ? and score = ?"; 77. PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql4); 78. pstmt.setInt(1, upsno); 79. pstmt.setString(2,upcname); 80. pstmt.setInt(3, upscore); 81. pstmt.executeUpdate(); 82. pstmt.close(); 83. System.out.println("修改成功"); 84. break; 85. } 86. } catch (SQLException e) { 87. // TODO Auto-generated catch block 88. e.printStackTrace(); 89. System.out.println("连接失败"); 90. } 91. 92. } 93. }