java和sql的连接,如何让Java写的程序和SQL2008数据库连接起来

当前位置:我的异常网» Eclipse » 如何让Java写的程序和SQL2008数据库连接起来

如何让Java写的程序和SQL2008数据库连接起来

www.myexceptions.net  网友分享于:2013-10-24  浏览:137次

怎么让Java写的程序和SQL2008数据库连接起来

毕业设计要求写一个物业管理系统 现在就差程序演示了

数据库这块老是出问题 我用的是Eclipse和SQL2008怎么让他们连接起来呢

求大神帮忙 我们后天就要答辩了 小弟感激不尽啊

09290626.gif

文件有点大 最好能留个邮箱

连接数据库文件代码如下:

//连接并初始化数据库

import java.sql.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

/**

* 原始的mssql连接文件

* @author 字母花园

*

*/

class mssql_ConnectServer

{

public static Connection connect(String url,String userName,String password)//连接数据库

{

Connection con=null;//为了建立连接而声名的引用

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//ODBC连接数据库

//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//JDBC连接数据库

con=DriverManager.getConnection(url,userName,password);//初始化数据库

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null,"数据库连接错误");//显示出错对话框

e.printStackTrace();

}

return con;

}

public static void initDataBase()//初始化数据库

{

Connection con=connect("jdbc:odbc:yzgl","","");//ODBC初始化数据库

//Connection con=connect("jdbc:microsoft:sqlserver://localhost:1433;databasename=master","sa","");//JDBC初始化数据库

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

String url = "jdbc:odbc://127.0.0.1:1433/yzgl";

String userName = "aaaa";

String password = "1111";

Statement stmt=null;//可执行SQL语句的引用

ResultSet rs=null;//执行SQL语句返回结果的引用

try

{

if(con!=null)//如果连接数据库成功

{

stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//创建SQL语句,设置窗口光标上下可以滚动

rs=stmt.executeQuery("select * from sysdatabases where name='yzgl'");//执行SQL语句

if(!rs.next())//next()代表光标往下一条记录,如果有内容那么为真,加上!就为假,那么就不建库了;如果没内容那就为假,加上!就为真,那么就可以建库了

{

URL url1=ClassLoader.getSystemResource("");

File file=new File(url1.getPath());

String dir=file.getAbsolutePath();

String sql="create database yzgl on primary(name='yzgl_data',filename='"

+dir+"\\..\\database\\yzgl_data.mdf',size=1,maxsize=50,filegrowth=10%) log on (name='yzgl_log',filename='"

+dir+"\\..\\database\\yzgl_log.ldf',size=1,maxsize=UNLIMITED,filegrowth=10%)";

stmt.execute(sql);//执行SQL语句

}

}

}

catch(SQLException e)

{

e.printStackTrace();

}

finally

{

try

{

rs.close();//关闭数据库,倒着关

stmt.close();

con.close();

}

catch(Exception e)

{

}

}

}

public static void initTable()//初始化表

{

Connection con=connect("jdbc:odbc:yzgl","","");

//Connection con=connect("jdbc:microsoft;sqlserver://localhost:1433;databasename=yzgl","sa","");

Statement stmt=null;

ResultSet rs=null;

try

{

if(con!=null)

{

stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

rs=stmt.executeQuery("select * from yzgl where name='house'");

if(!rs.next())

{

String sql="create table house";

sql=sql+"(houseID int primary key,";

sql=sql+"houseingID int not null,houseUnit char(10) not null,houseFloor char(10) not null,houseArea char(10) not null)";

stmt.execute(sql);

}

rs=stmt.executeQuery("select * from sysobjects where name='owner'");

if(!rs.next())

{

String sql="create table owner";

sql=sql+"(ownerID int identity(1,1) primary key,";

sql=sql+"ownerName char(10) not null,ownerSex char(2) not null,";

sql=sql+"ownerTel char(15) not null,ownerAddress char(30) not null,";

sql=sql+"houseID int references house(houseID),moveinDate char(15) not null,memo char(50),carNumber char(5),ownerPhoto image)";

stmt.execute(sql);

}

rs=stmt.executeQuery("select * from yzgl where name='charge1'");

if(!rs.next())

{

String sql="create table charge1";

sql=sql+"(ownerName char(10) primary key,";

sql=sql+"houseID int references house(houseID),chargeName char(20) not null,much char(10) not null,money char(10) not null,date char(12) not null,";

sql=sql+"receiver char(10) not null)";

stmt.execute(sql);

}

rs=stmt.executeQuery("select * from yzgl where name='charge2'");

if(!rs.next())

{

String sql="create table charge2";

sql=sql+"(ownerName char(10) primary key,";

sql=sql+"houseID int references house(houseID),chargeName char(20) not null,much char(10) not null,money char(10) not null,date char(12) not null,";

sql=sql+"receiver char(10) not null)";

stmt.execute(sql);

}

rs=stmt.executeQuery("select * from yzgl where name='charge3'");

if(!rs.next())

文章评论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值