数据库这块老是出问题 我用的是Eclipse和SQL2008怎么让他们连接起来呢
求大神帮忙 我们后天就要答辩了 小弟感激不尽啊
文件有点大 最好能留个邮箱
连接数据库文件代码如下:
//连接并初始化数据库
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())
{
String sql="create table charge3";
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='charge4'");
if(!rs.next())
{
String sql="create table charge4";
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='repair'");
if(!rs.next())
{
String sql="create table repair";
sql=sql+"(repairID int identity(1,1) primary key,";
sql=sql+"houseID int references house(houseID),repairItem char(100) not null,repairDate char(15) not null,finish char(10),receiver char(100))";
stmt.execute(sql);
}
rs=stmt.executeQuery("select * from yzgl where name='complain'");
if(!rs.next())
{
String sql="create table complain";
sql=sql+"(complainID int identity(1,1) primary key,";
sql=sql+"houseID int references house(houseID),complainItem char(100) not null,complainDate char(15) not null,finish char(10),receiver char(100))";
stmt.execute(sql);
}
rs=stmt.executeQuery("select * from yzgl where name='usertable'");
if(!rs.next())
{
String sql="create table usertable";
sql=sql+"(userName char(5) primary key,";
sql=sql+"userPassword char(16) not null)";
stmt.execute(sql);
sql="insert into usertable(userName,userPassword) values('aaaa','1111')";
stmt.execute(sql);
}
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
}
}
}
}
db.properties 文件配置:
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc://127.0.0.1:1433/yzgl";
userName = "aaaa";
password = "1111";
求各位大神指点迷津。
5 个解决方案
#1
package database;
import java.sql.Connection;
import java.sql.DriverManager;
public class MyDB{
private Connection conn=null;
private String jdbcUrl="jdbc:sqlserver://localhost:1433;database=manger";
private String user="sa";
private String password="123456";
public Connection getConn(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(jdbcUrl, user,password);
}
catch(Exception e){
e.printStackTrace();
}
return conn;
}
public void closeConn(){
try{
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
#2
你要把错误贴出来。。。
#3
感谢大神解答
#4
额 不好意思 我是新手 不过毕业设计已经搞定 还是要谢谢你的回帖 看帖回帖是种美德
#5
楼主 可以把你的数据库发我用下嘛?
#1
package database;
import java.sql.Connection;
import java.sql.DriverManager;
public class MyDB{
private Connection conn=null;
private String jdbcUrl="jdbc:sqlserver://localhost:1433;database=manger";
private String user="sa";
private String password="123456";
public Connection getConn(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(jdbcUrl, user,password);
}
catch(Exception e){
e.printStackTrace();
}
return conn;
}
public void closeConn(){
try{
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
#2
你要把错误贴出来。。。
#3
感谢大神解答
#4
额 不好意思 我是新手 不过毕业设计已经搞定 还是要谢谢你的回帖 看帖回帖是种美德
#5
楼主 可以把你的数据库发我用下嘛?