import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class zmysql {
private Connection con;
public zmysql(){
String classforname="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String servanddb="jdbc:sqlserver://localhost:1433;databaseName=zxl";
String user="sa";
String pwd="zxl";
try{
Class.forName(classforname);
con= DriverManager.getConnection(servanddb, user, pwd);
System.out.println(" run sql ");
}catch(Exception e){
e.printStackTrace();
}
}
public boolean selectdb(String name,String pub){
try{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from zUser where zusername='"+name+"' and zpassword='"+pub+"'");
if(rs.next()) {
String zname = rs.getString("zusername");
String pass = rs.getString("zpassword");
System.out.println(zname+" "+pass+" login ok!");
rs.close();
con.close();
return true;
}else{
System.out.println("not login!");
rs.close();
con.close();
return false;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public static void main(String[] args){
zmysql z=new zmysql();
if(z.selectdb("user", "pass")){
System.out.println("ok");
}else{
System.out.println("not ok");
}
}
}
驱动:
sqljdbc4.jar
sqljdbc.jar
/*
Navicat MySQL Data Transfer
Source Server : .
Source Server Version : 50051
Source Host : localhost:3306
Source Database : zzz
Target Server Type : MYSQL
Target Server Version : 50051
File Encoding : 65001
Date: 2013-12-08 15:23:44
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `zuser`
-- ----------------------------
DROP TABLE IF EXISTS `zuser`;
CREATE TABLE `zuser` (
`zusername` char(11) NOT NULL,
`zpassword` char(11) NOT NULL,
`zsex` char(2) default NULL,
`zemail` char(20) default NULL,
`zphone` char(11) default NULL,
`zuser` int(11) NOT NULL,
`c` bigint(20) NOT NULL auto_increment,
PRIMARY KEY (`c`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of zuser
-- ----------------------------
INSERT INTO `zuser` VALUES ('user', 'pass', '男', '771948524@qq.com', '18254598113', '0', '1');
INSERT INTO `zuser` VALUES ('user1', 'pass1', '女', '1@qq.com', '18254598114', '1', '2');
INSERT INTO `zuser` VALUES ('user2', 'pass2', '男', '2@qq.com', '18254598115', '0', '3');