JDBC之连接sql server数据库
作者:互联网
1.添加JDBC的驱动包
新建package命名为lib,将驱动包放入lib中,并build path。
驱动包下载地址:
链接:https://pan.baidu.com/s/1nSJLFx73-S4QsdH168GKbw
提取码:e0rh
2..创建jdbc.properties,将数据库配置写入该文件中
user=sa password=数据库密码 url=jdbc:sqlserver://localhost:端口号(默认是1433);DatabaseName=数据库名称 driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
3.创建一个类,用于获取连接
public static Connection getConnection() throws Exception { InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc1/jdbc.properties"); Properties pros = new Properties(); pros.load(is); String user = pros.getProperty("user"); String password = pros.getProperty("password"); String url = pros.getProperty("url"); String driverClass = pros.getProperty("driverClass"); Class.forName(driverClass); Connection conn = DriverManager.getConnection(url,user,password); return conn; }
4.测试连接
@Test public void connectinTest() throws Exception { Connection conn = getConnection(); System.out.println(conn); conn.close(); }
标签:jdbc,String,pros,server,driverClass,JDBC,user,sql,conn 来源: https://www.cnblogs.com/hxnp/p/16525621.html