1、新建项目,导入包 sqljdbc4.jar或sqljdbc.jar(jdk1.7版本)
2、新建类文件ConnectionDB.java
package hello; import java.sql.*; /*
* 连接数据库类
*/
public class ConnectionDB { public ConnectionDB() { } // 建立与驱动包的联系
private static final String Drive = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 数据库地址
private final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=English"; // 用户名
private final String USER = "sa"; // 密码
private final String PASSWORD = "940523"; Connection conn = null; // 定义数据库连接对象 Statement stmt = null; // 定义SQL命令集对象 ResultSet rs = null; // 定义结果集对象 /**
* 连接数据库
*
*/
private Connection getConnection() {
try {
// 加载驱动
Class.forName(Drive);
// 连接数据库
conn = DriverManager.getConnection(URL, USER, PASSWORD);
return conn;
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
return null;
} /**
* 数据库查询操作
*
*/
public void getDateByQuery(String sql) {
try {
// 获取盛装SQL语句的载体
stmt = this.getConnection().createStatement();
// 获取结果集rs
rs = stmt.executeQuery(sql);
// 输出查询结果
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2)
+ " " + rs.getString(3));
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库查询出错!");
}
} /**
* 数据库更新操作
*
*/
public void getDataByUpdate(String sql) {
try {
// 获取盛装SQL语句的载体
stmt = this.getConnection().createStatement();
// 获得更新记录条数
int line = stmt.executeUpdate(sql);
System.out.println("更新了" + line + "条记录\n");
// 查询数据库
String sqlQuery = "select *from word";
rs = stmt.executeQuery(sqlQuery);
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2)
+ " " + rs.getString(3));
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库更新出错!");
} } /**
* 数据库插入操作
*
*/
public void getDataByInsert(String sql) {
try {
stmt = this.getConnection().createStatement();
int line = stmt.executeUpdate(sql);
System.out.println("插入了" + line + "条记录\n");
String sqlQuery = "select *from word";
rs = stmt.executeQuery(sqlQuery);
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2)
+ " " + rs.getString(3));
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库插入出错!");
} } /**
* 数据库删除操作
*/
public void getDataByDelete(String sql) {
try {
stmt = this.getConnection().createStatement();
int line = stmt.executeUpdate(sql);
System.out.println("删除了" + line + "条记录\n");
String sqlQuery = "select *from word";
rs = stmt.executeQuery(sqlQuery);
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2)
+ " " + rs.getString(3));
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库删除出错!");
} } /**
* 关闭数据库连接
*/
public void close() {
try {
if (rs != null) {
rs.close(); // 关闭结果集
} if (stmt != null) {
stmt.close();// 关闭盛装SQL语句的载体
} if (conn != null) {
conn.close();// 关闭数据库连接
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("关闭连接出错!");
System.exit(0);
}
} }
简版:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; public class Test { public static void main(String[] args) {
try {
// 1.注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 2.建立连接
// 2.1连接串
String conStr = "jdbc:sqlserver://localhost:1433;DatabaseName=JavaDB"; Connection con = DriverManager.getConnection(conStr, "sa",
"123456");
// 创建会话
Statement st = con.createStatement();
// String sql = "select count(*) as cnt from T_user where username='"
// + username + "' and password='" + password + "'";
String sql = "select count(*) as cnt from T_user";
// 结果集
ResultSet rs = st.executeQuery(sql);
rs.next();
// if (rs.getInt("cnt") > 0) {
// System.out.println("登录成功!");
// } else {
// System.out.println("登录失败!");
// }
} catch (Exception ex) {
ex.printStackTrace();
}
} }
3、使用方法:
public class Main { public static void main(String[] args) {
ConnectionDB db = new ConnectionDB();
String QuerySql = "select * from word where id=1";
db.getDateByQuery(QuerySql);
// String insertSql="insert into word values('134','boy','男孩')";
// db.getDataByInsert(insertSql); } }