java 连接数据库

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值