JDBC连接MYSQL面试题(标准模板)
package cn.mldn.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Random; /** * @author 风 *按照要求模拟学生信息录入系统(24分): 在项目根目录下新建info.txt文件(文件内容见备注),运用IO流知识将info.txt里的学生姓名读取到项目中 ,为每个姓名生成对应的年龄(年龄用10到30之间的随机数表示), 将学生姓名和年龄存入数据库中:可以用自己的sqlyog来创建数据库以及表结构, 数据库名为studb,表名为student,字段为name(varchar),age(int)。数据存储到数据库后,完成以下操作: a.统计年龄在10到20之间的学生有多少人; b.查询出姓名以L开头的学生姓名; c.将kitty的年龄修改为24岁; d.按照年龄的降序从数据库中查询学生姓名并将学生姓名存入任意集合中并在控制台打印出来; 提示: 生成[m,n]之间的随机数可参照以下代码: Random random = new Random(System.in); random.nextInt(n-m+1)+m; 比如:20-30之间: random.nextInt(30-20+1)+20; 备注: info.txt内容如下: Lily andi Locy kitty */ public class TestDemo2{ public static void main(String[] args) throws Exception{ // Test1(); Test2(); Test3(); Test4(); Test5(); } public static void Test1() throws Exception{ System.out.println("运用IO流知识将info.txt里的学生姓名读取到项目中 ,为每个姓名生成对应的年龄(年龄用10到30之间的随机数表示)"); String encoding="GBK"; File f=new File("D:"+File.separator+"testdemo"+File.separator+"MyProject"+File.separator+"info.txt"); InputStreamReader read = new InputStreamReader(new FileInputStream(f),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); Statement stmt=conn.createStatement(); String lineTxt = null; String name=""; int age=0; while((lineTxt = bufferedReader.readLine()) != null){ name=lineTxt; //这块写获取随机数 age=随机数 int n=30; int m=10; Random random = new Random(); age=random.nextInt(n-m+1)+m; String sql1="insert into student values ('"+name+"','"+age+"')"; stmt.executeUpdate(sql1); System.out.println("名字:"+lineTxt+"; 年龄:"+age); //插入数据库 } read.close(); stmt.close(); conn.close(); } public static void Test2() throws Exception{ System.out.println("a.统计年龄在10到20之间的学生有多少人;"); Connection con=null; Statement stmt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); stmt=con.createStatement(); String sql2="SELECT COUNT(age) FROM student WHERE age BETWEEN 10 AND 20;"; rs=stmt.executeQuery(sql2); rs.last(); System.out.println(rs.getRow()); } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); }finally{ if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(con!=null){con.close();} } } public static void Test3() throws Exception{ System.out.println("b.查询出姓名以L开头的学生姓名;"); Connection con=null; PreparedStatement ps=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); String sql3="select * from student where name like ?"; ps=con.prepareStatement(sql3); ps.setString(1, "L%"); rs=ps.executeQuery(); while(rs.next()){ String name=rs.getString(1); System.out.println(name); } } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } finally{ if(rs!=null){rs.close();} if(ps!=null){ps.close();} if(con!=null){con.close();} } } public static void Test4() throws Exception{ System.out.println("c.将kitty的年龄修改为24岁;"); Connection con=null; Statement stmt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); stmt=con.createStatement(); String sql4="update student set age=24 where name='kitty'"; stmt.executeUpdate(sql4); String sql="select*from student where name='kitty'"; rs=stmt.executeQuery(sql); while(rs.next()){ String age=rs.getString(2); System.out.println("kitty的年龄是:"+age); } } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } finally{ if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(con!=null){con.close();} } } public static void Test5() throws Exception{ System.out.println("d.按照年龄的降序从数据库中查询学生姓名并将学生姓名存入任意集合中并在控制台打印出来;"); Connection con=null; Statement stmt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); stmt=con.createStatement(); String sql5="select *from student order by age desc "; rs=stmt.executeQuery(sql5); while(rs.next()){ String age=rs.getString(2); System.out.println(age); } } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } finally{ if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(con!=null){con.close();} } } }
package cn.mldn.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Random; /** * @author 风 *按照要求模拟学生信息录入系统(24分): 在项目根目录下新建info.txt文件(文件内容见备注),运用IO流知识将info.txt里的学生姓名读取到项目中 ,为每个姓名生成对应的年龄(年龄用10到30之间的随机数表示), 将学生姓名和年龄存入数据库中:可以用自己的sqlyog来创建数据库以及表结构, 数据库名为studb,表名为student,字段为name(varchar),age(int)。数据存储到数据库后,完成以下操作: a.统计年龄在10到20之间的学生有多少人; b.查询出姓名以L开头的学生姓名; c.将kitty的年龄修改为24岁; d.按照年龄的降序从数据库中查询学生姓名并将学生姓名存入任意集合中并在控制台打印出来; 提示: 生成[m,n]之间的随机数可参照以下代码: Random random = new Random(System.in); random.nextInt(n-m+1)+m; 比如:20-30之间: random.nextInt(30-20+1)+20; 备注: info.txt内容如下: Lily andi Locy kitty */ public class TestDemo2{ public static void main(String[] args) throws Exception{ // Test1(); Test2(); Test3(); Test4(); Test5(); }
// public static void Test1() throws IOException, ClassNotFoundException, SQLException{
// BufferedReader br=new BufferedReader(new FileReader("info.txt"));
// int m=30;
// int n=10;
// String sql=null;
// String name=null;
// Connection con=null;
// PreparedStatement ps=null;
// ResultSet rs=null;
// while((name=br.readLine())!=null){
// Random rd=new Random();
// int age=rd.nextInt(m-n+1)+10;
// sql="insert into student values('"+name+"','"+age+"')";
// System.out.println(sql);
// Class.forName("com.mysql.jdbc.Driver");
// con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123");
// ps=con.prepareStatement(sql);
// ps.executeUpdate();
// }
// ps.close();
// con.close();
// br.close();
// } public static void Test1() throws Exception{ System.out.println("运用IO流知识将info.txt里的学生姓名读取到项目中 ,为每个姓名生成对应的年龄(年龄用10到30之间的随机数表示)"); String encoding="GBK"; File f=new File("D:"+File.separator+"testdemo"+File.separator+"MyProject"+File.separator+"info.txt"); InputStreamReader read = new InputStreamReader(new FileInputStream(f),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); Statement stmt=conn.createStatement(); String lineTxt = null; String name=""; int age=0;
int n=30;
int m=10; while((lineTxt = bufferedReader.readLine()) != null){ name=lineTxt; //这块写获取随机数 age=随机数 Random random = new Random(); age=random.nextInt(n-m+1)+m; String sql1="insert into student values ('"+name+"','"+age+"')"; stmt.executeUpdate(sql1); System.out.println("名字:"+lineTxt+"; 年龄:"+age); //插入数据库 } read.close(); stmt.close(); conn.close(); } public static void Test2() throws Exception{ System.out.println("a.统计年龄在10到20之间的学生有多少人;"); Connection con=null; Statement stmt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); stmt=con.createStatement(); String sql2="SELECT COUNT(age) FROM student WHERE age BETWEEN 10 AND 20;"; rs=stmt.executeQuery(sql2); rs.last(); System.out.println(rs.getRow()); } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); }finally{ if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(con!=null){con.close();} } } public static void Test3() throws Exception{ System.out.println("b.查询出姓名以L开头的学生姓名;"); Connection con=null; PreparedStatement ps=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); String sql3="select * from student where name like ?"; ps=con.prepareStatement(sql3); ps.setString(1, "L%"); rs=ps.executeQuery(); while(rs.next()){ String name=rs.getString(1); System.out.println(name); } } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } finally{ if(rs!=null){rs.close();} if(ps!=null){ps.close();} if(con!=null){con.close();} } } public static void Test4() throws Exception{ System.out.println("c.将kitty的年龄修改为24岁;"); Connection con=null; Statement stmt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); stmt=con.createStatement(); String sql4="update student set age=24 where name='kitty'"; stmt.executeUpdate(sql4); String sql="select*from student where name='kitty'"; rs=stmt.executeQuery(sql); while(rs.next()){ String age=rs.getString(2); System.out.println("kitty的年龄是:"+age); } } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } finally{ if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(con!=null){con.close();} } } public static void Test5() throws Exception{ System.out.println("d.按照年龄的降序从数据库中查询学生姓名并将学生姓名存入任意集合中并在控制台打印出来;"); Connection con=null; Statement stmt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root","123"); stmt=con.createStatement(); String sql5="select *from student order by age desc "; rs=stmt.executeQuery(sql5); while(rs.next()){ String age=rs.getString(2); System.out.println(age); } } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } finally{ if(rs!=null){rs.close();} if(stmt!=null){stmt.close();} if(con!=null){con.close();} } } }