在Eclipse中实现Jdbc连接SQL Server2005数据库

        在SQL Server 2005数据库中创建一张表

1.首先SQL Server2005中创建一个数据库TestDB,用户名为:sa,密码为123;

2.下载sqljdbc4驱动程序,打开Eclipse,在Java构建路径的库中添加此驱动程序;

3.新建一个JDBC_Test项目,添加一个类CreateTable:

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class CreateTable {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//声明JDBC驱动程序类型
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";

//定义JDBC的Url对象
String conURL="jdbc:sqlserver://localhost:1433;databaseName=TestDB;user=sa;password=123";

try
{
//加载JDBC驱动程序
Class.forName(JDriver);
}
catch(java.lang.ClassNotFoundException e){
System.out.println("无法加载JDBC驱动程序."+e.getMessage());

}

Connection con=null;
Statement s=null;
try
{
//连接数据库Url
con=DriverManager.getConnection(conURL);
//建立Statement类对象
s=con.createStatement();
//创建一个含有3个字段的顾客表costomer
String query="create table customer(" + "id char(10) not null,"+"name char(15),"+"totalmoney float"+")";
s.executeUpdate(query); //执行SQL语句

System.out.println("创建表成功!");
}
catch(SQLException e){
System.out.println("SQLException:"+e.getMessage());
}
finally
{
try
{
if(s!=null)
{
s.close();
s=null;

}
if(con!=null)
{
con.close(); //关闭与数据库的连接
con=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}



}


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值