jdbc连接数据库mssql初步学习
数据库名称sa
密码sa
数据库db_test
表user
package jdbcTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class JdbcTest {
/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
// TODO Auto-generated method stub
//add();
//get();
//update();
delete();
}
public static void add() throws ClassNotFoundException, SQLException {
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=db_test";
String username = "sa";
String password = "sa";
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// 加载驱动程序
Class.forName(driverName);
// 创建连接对象
Connection conn = DriverManager.getConnection(url, username, password);
// 定义sql语句
String sql = "insert into [user](name,password,email,age,birthday,money)"
+ " values('john','123','john@126.com',23,'1990-12-23',2343.12)";
// 创建语句对象,执行sql语句集并得到结果
Statement st = conn.createStatement();
st.executeUpdate(sql);
// 关闭资源对象
conn.commit();
st.close();
conn.close();
}
public static void get() throws ClassNotFoundException, SQLException {
// 定义数据库访问参数
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String user = "sa";
String password = "sa";
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=db_test";
// 加载驱动
Class.forName(driverName);
// 创建连接对象
Connection conn = DriverManager.getConnection(url, user, password);
// 定义SQL语句
String sql = "select id,name,password,email,birthday,money from [user]";
// 创建语句对象
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
//遍历结果集
while(rs.next()){
int id= rs.getInt("id");
String name =rs.getString("name");
String pass=rs.getString("password");
String email = rs.getString("email");
Date birthday =rs.getDate("birthday");
float money= rs.getFloat("money");
System.out.println("id是:"+id+"姓名是: "+name+" 密码是:"+pass+"邮箱是:"
+email+"生日是: "+birthday+"工资是"+money);
}
// 关闭资源对象
rs.close();
st.close();
conn.close();
}
public static void update() throws ClassNotFoundException, SQLException {
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=db_test";
String username = "sa";
String password = "sa";
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// 加载驱动程序
Class.forName(driverName);
// 创建连接对象
Connection conn = DriverManager.getConnection(url, username, password);
// 定义sql语句
String sql = "update [user] set name='lucy',password='123',money=5000 where id=1";
// 创建语句对象,执行sql语句集并得到结果
Statement st = conn.createStatement();
st.executeUpdate(sql);
// 关闭资源对象
conn.commit();
st.close();
conn.close();
}
public static void delete() throws ClassNotFoundException, SQLException {
String url = "jdbc:sqlserver://localhost:1433;DataBaseName=db_test";
String username = "sa";
String password = "sa";
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// 加载驱动程序
Class.forName(driverName);
// 创建连接对象
Connection conn = DriverManager.getConnection(url, username, password);
// 定义sql语句
String sql = "delete from [user] where id=1";
// 创建语句对象,执行sql语句集并得到结果
Statement st = conn.createStatement();
st.executeUpdate(sql);
// 关闭资源对象
conn.commit();
st.close();
conn.close();
}
}
注意:以上导包需要三个,可以在下面下载。
如果是微软最新的包,可以只下载sql2005jdbc.jar