开学测试
王老师的开学测试和上学期题目类似,但是要套用模板,模板没看懂而且我也只会我那一套固定模式,导致我没有正常完成登陆在登陆上用了大把时间还是没有做出来,最后只能匆匆完成了征集表和注册页面,注册可以正常完成,但是征集表内容不能正常连接数据库不能提交而且没有完成设置的各种限制,比如项目描述不能超过五百字,手机号码只能是十一位。确认密码也没有做出来和原密码一致。
感觉模板注释太少,对初学者来说很难看懂。如果多一点注释的话可能会比较容易。
Data.uil.java
package util; import java.sql.*; public class Data_uil { public Connection getConnection() { try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch(ClassNotFoundException e) { e.printStackTrace(); } String user="sa"; String password="364624"; String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=XVQIU"; Connection con=null; try{ con=DriverManager.getConnection(url,user,password); }catch(SQLException e) { e.printStackTrace(); } return con; } public String selectPassword(String username) { Connection connection=getConnection(); String sql="select *from login where username=?"; PreparedStatement preparedStatement=null; ResultSet result=null; String password=null; try{ preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,username); result=preparedStatement.executeQuery();//可执行的 查询 if(result.next()) password=result.getString("userpassword"); }catch(SQLException e){ e.printStackTrace(); }finally { close(preparedStatement); close(result); close(connection); } System.out.println("找到的数据库密码为:"+password); return password; } public void close (Connection con) { try{ if(con!=null) { con.close(); } }catch(SQLException e) { e.printStackTrace(); } } public void close (PreparedStatement preparedStatement) { try{ if(preparedStatement!=null) { preparedStatement.close(); } }catch(SQLException e) { e.printStackTrace(); } } public void close(ResultSet resultSet) { try{ if(resultSet!=null) { resultSet.close(); } }catch(SQLException e) { e.printStackTrace(); } } }
Test.java
package util; import util.util; import java.sql.*; import java.util.Scanner; public class Test { static Connection conn; static PreparedStatement ps = null; static ResultSet rs; static String sql = "select * from login"; static util ut= new util(); static Scanner in = new Scanner(System.in); static String username; static String userpassword; static String phone; static String danwei; public static String a_username() { return username; } public static String a_userpassword() { return userpassword; } public static String a_phone() { return phone; } public static String a_danwei() { return danwei; } public static int add(String username,String userpassword,String phone,String danwei) { conn= ut.getConn(); String sql="insert into login values(?,?,?,?)"; int b=0; try { ps=conn.prepareStatement(sql); ps.setString(1,username); ps.setString(2,userpassword); ps.setString(3,phone); ps.setString(4,danwei); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("添加成功"); } else { System.out.println("添加失败"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static void main(String[] args) { //System.out.println("请输入要删除课程"); //System.out.println("请输入查找课程:"); //String a; //Scanner scan=new Scanner(System.in); //a=scan.next(); //delete(a); //findtwo(a); } }
Test1.java
package util; import util.util; import java.sql.*; import java.util.Scanner; public class Test1 { static Connection conn; static PreparedStatement ps = null; static ResultSet rs; static String sql = "select * from zhengjibiao"; static util ut= new util(); static Scanner in = new Scanner(System.in); static String name; static String describe; static String type; static String mode; static String money; public static String a_name() { return name; } public static String a_describe() { return describe; } public static String a_type() { return type; } public static String a_mode() { return mode; } public static String a_money() { return money; } public static int add(String name,String describe,String type,String mode,String money) { conn= ut.getConn(); String sql="insert into zhengjibiao values(?,?,?,?,?)"; int b=0; try { ps=conn.prepareStatement(sql); ps.setString(1,name); ps.setString(2,describe); ps.setString(3,type); ps.setString(4,mode); ps.setString(5,money); int a=ps.executeUpdate(); if(a>0) { b++; System.out.println("添加成功"); } else { System.out.println("添加失败"); } }catch(Exception e) { e.printStackTrace(); } try { if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e2) { e2.printStackTrace(); } return b; } public static void main(String[] args) { //System.out.println("请输入要删除课程"); //System.out.println("请输入查找课程:"); //String a; //Scanner scan=new Scanner(System.in); //a=scan.next(); //delete(a); //findtwo(a); } }
util.java
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class util { String user="sa"; String password="364624"; String url="jdbc:sqlserver://localhost:1433;DatabaseName=XVQIU"; public Connection getConn(){ Connection conn=null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn=DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public void close(ResultSet rs, Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch(SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch(SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch(SQLException e) { e.printStackTrace(); } } } }