首页 > 代码库 > JDBC NOTE

JDBC NOTE

JDBC 基本流程:

1. 加载驱动

2.得到连接

3.创建PreparedStatement

4.执行操作

5.根据结果做处理

 

1. SQLSERVER

 1     String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; 2         String dbURL="jdbc:sqlserver://localhost:1433; DatabaseName=UserManager"; 3         String sqlUserName="sa"; 4         String userPwd="wfjts12332100"; 5         Connection dbConn=null; 6         ResultSet rs=null; 7         PreparedStatement ps=null; 8         try 9         {10             Class.forName(driverName);11             dbConn=DriverManager.getConnection(dbURL,sqlUserName,userPwd);12             System.out.println("Connection Successful!");13             ps=dbConn.prepareStatement("select * from users where id=? and passwd=? ");14             ps.setObject(1, userName);15             ps.setObject(2, password);16             rs=ps.executeQuery();17             if(rs.next())18             {19                 System.out.println("用戶合法");20             }21         }22         catch(Exception ex)23         {24             ex.printStackTrace();25         }
View Code

 

JDBC NOTE