时间:2021-07-01 10:21:17 帮助过:4人阅读
导包:jtds-1.2.2.jar public class DBUtil { /** * 获得数据库连接 * @return */ public static ConnectiongetConnection() { Connectionconn = null ; try { // 1. 加载驱动 Class. forName ( net.sourceforge.jtds.jdbc.Driver ); // 连接字符串 jdbc:jtd
导包:jtds-1.2.2.jar
publicclassDBUtil {
/**
* 获得数据库连接
* @return
*/
publicstatic ConnectiongetConnection() {
Connectionconn = null;
try {
// 1. 加载驱动
Class.forName("net.sourceforge.jtds.jdbc.Driver");
// 连接字符串"jdbc:jtds:sqlserver://IP:端口/数据库名";
final String url ="jdbc:jtds:sqlserver://127.0.0.1:1433/SuperDogMall";
// 登陆数据库账号
final String user ="sa";
// 登陆数据库密码
final String password ="123456";
conn= DriverManager.getConnection(url, user, password);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
publicstatic void main(String[] args) {
System.out.println(DBUtil.getConnection());
}
}
部分建表和插入内容的SQL语句:
use SuperDogMall
--建立表userinfo
create tableuserinfo
( userIdvarchar(20)primary key,
userPassword varchar(20)not null,
userName varchar(20),
userEmail varchar(40),
userBirth datetime,
userAddress varchar(40),
userMoney int
)
select *from userinfo
insert intouserinfo values('zhizunbao','123','至尊宝','zhizunbao@supermall.com','1980-10-10','四川省九寨沟',50000)
insert intouserinfo values('ershixiong','123','二师兄','ershixiong@supermall.com','1981-11-14','山东省威海市',5000)
insert intouserinfo values('zixiaxianzi','123','紫霞仙子','zixiaxianzi@supermall.com','1991-11-14','四川省盘丝洞',5800)
insert intouserinfo values('niumowang','123','牛魔王','niumowang@supermall.com','1980-11-14','四川省牛头山',5100)
insert intouserinfo values('tieshan','123','铁扇公主','tieshan@supermall.com','1988-11-14','新疆省火焰山',15000)
create tableproduct
(
proId varchar(20)primary key,
proName varchar(20),
proType varchar(20),
proPrice float check(proPrice>=0),
proStore int check(proStore>=0),
proSales int check(proSales>=0),
proPicture varchar(60),
proDesc varchar(200)
)
select *from product
insert intoproduct values('p_001','狗不理包子','食物','5','3000','1000','','好吃')
insert intoproduct values('p_002','庆丰包子','食物','2','3000','1500','','好吃')
insert into product values('p_003','德园包子','食物','1','2000','1000','','好吃')
一些driver和url的写法:
oracle
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:数据库名"
sqlserver
driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名"
mysql
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/数据库名?[后接参数]"
db2
driver="com.ibm.db2.jdbc.app.DB2Driver"
url="jdbc:db2://localhost:5000/数据库名"
sybase
driver="com.sybase.jdbc.SybDriver"
url="jdbc:sybase:Tds:localhost:5007/数据库名"