在eclipse里创建数据库脚本语言时(数据库.sql)出现错误

在eclipse里创建数据库脚本语言时(数据库.sql)出现错误,第1张

1. 路径存在 空格 可能存在问题

2. 使用汉字作为文件名字可能存在问题

3. Eclipse 相应配置可能存在问题

给的提示信息较少,无法准确找到原因 ,希望对你有所帮助!

1.把sqlserver的驱动jar包复制进WebRoot/WEB-INF/lib下面,。

2.右击项目名称,Build Path下好像有个Add External archives点击d出确认框,确认就OK了.

或者

右击项目名称,点击properties,选择Java Build Path选项卡(左边的),在对应的右边的界面选择Libraries,

会看到有Add JARs,Add External JARs,Add Library等按钮,点击Add JARs就可以选择项目中WEB-INF/lib里面的包,然后导入。就可以加载驱动了。

给一个很简单的例子你看一下:用Java两层实现的。

1.数据库脚本

use master

go

--创建数据库

if exists (select * from sysdatabases where name = 'StudentManage')

drop database StudentManage

go

create database StudentManage

go

--使用数据库

use StudentManage

go

--创建表

--创建学生信息表

if exists (select * from sysobjects where name = 'StuInfo')

drop table StuInfo

go

create table StuInfo

(

stuId int identity(1,1) primary key, --学生编号

stuName varchar(20), --学生姓名

stuSex char(2),--学生性别

stuSeat int, --学生座位

)

go

--插入测试数据

insert into StuInfo values('曾哥','男',1)

insert into StuInfo values('春哥','男',2)

insert into StuInfo values('凤姐','女',3)

insert into StuInfo values('潇洒哥','男',4)

go

select * from stuInfo

①:获取数据库的连接类

package com.svse.util

import java.sql.Connection

import java.sql.DriverManager

import java.sql.ResultSet

import java.sql.SQLException

import java.sql.Statement

/**

* 1.获得数据库连接对象 2.关闭连接资源对象

*

* @author Administrator

*

*/

public class DBHelper {

// 4个连接字符串driver,url,username,password

private final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"

private final static String URL = "jdbc:sqlserver://localhost:1433databasename=StudentManage"

private final static String USERNAME = "sa"

private final static String PASSWORD = "sa"

/**

* 获得数据库连接对象

*

* @return

*/

public static Connection getConnection() {

Connection conn = null

try {

Class.forName(DRIVER)

conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)

} catch (ClassNotFoundException e) {

e.printStackTrace()

} catch (SQLException e) {

e.printStackTrace()

}

return conn

}

/**

* 关闭资源

*

* @param rs

* @param st

* @param conn

*/

public static void closeResource(ResultSet rs, Statement st, Connection conn) {

try {

if (rs != null) {

rs.close()

}

if (st != null) {

st.close()

}

if (conn != null) {

conn.close()

}

} catch (SQLException e) {

e.printStackTrace()

}

}

//用于测试数据库的连接

public static void main(String[] args) {

Connection conn = DBHelper.getConnection()

if (conn != null) {

System.out.println("OK")

} else {

System.out.println("ERROR")

}

}

}

②:学生实体类

package com.svse.entity

/**

* 实体类: 1.实体类对应数据库中的一张表 2.实体类的属性对应数据库中表的字段 3.一个实体类对象对应数据库中表的一条记录

*

* @author Administrator

*

*

*/

public class StuInfo {

private int stuId

private String stuName

private String stuSex

private int stuSeat

public StuInfo() {

}

public StuInfo(int stuId, String stuName, String stuSex, int stuSeat) {

this.stuId = stuId

this.stuName = stuName

this.stuSex = stuSex

this.stuSeat = stuSeat

}

public int getStuId() {

return stuId

}

public void setStuId(int stuId) {

this.stuId = stuId

}

public String getStuName() {

return stuName

}

public void setStuName(String stuName) {

this.stuName = stuName

}

public int getStuSeat() {

return stuSeat

}

public void setStuSeat(int stuSeat) {

this.stuSeat = stuSeat

}

public String getStuSex() {

return stuSex

}

public void setStuSex(String stuSex) {

this.stuSex = stuSex

}

}

③:数据访问层:对数据库的增删改查

package com.svse.dao

import java.sql.Connection

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

import java.util.ArrayList

import java.util.List

import com.svse.entity.StuInfo

import com.svse.util.DBHelper

/**

* 1.查询所有 2.根据ID查询 3.添加 4.删除 5.修改

*

* @author Administrator

*

*/

public class StuInfoDAO {

private Connection conn// 数据库连接对象

private PreparedStatement ps// 执行SQL语句对象

private ResultSet rs// 结果集对象

/**

* 1.查询所有学生信息

*

* @return

*/

public List<StuInfo>getAllStuInfo() {

conn = DBHelper.getConnection()

String sql = "select * from stuInfo"

List<StuInfo>list = new ArrayList<StuInfo>()

try {

ps = conn.prepareStatement(sql)

rs = ps.executeQuery()

while (rs.next()) {

int stuID = rs.getInt("stuId")

String stuName = rs.getString("stuName")

String stuSex = rs.getString("stuSex")

int stuSeat = rs.getInt("stuSeat")

// 创建实体类对象

StuInfo s = new StuInfo()

s.setStuId(stuID)

s.setStuName(stuName)

s.setStuSex(stuSex)

s.setStuSeat(stuSeat)

// 将实体类对象添加到集合类中

list.add(s)

}

} catch (SQLException e) {

e.printStackTrace()

} finally {

DBHelper.closeResource(rs, ps, conn)

}

return list

}

/**

* 2.根据学好查询学生信息

*

* @param stuId

* @return

*/

public StuInfo getStuInfoByStuId(int stuId) {

StuInfo s = null

conn = DBHelper.getConnection()

String sql = "select * from stuInfo where stuId = ?"

try {

ps = conn.prepareStatement(sql)

ps.setInt(1, stuId)

rs = ps.executeQuery()

while (rs.next()) {

int stuID = rs.getInt("stuId")

String stuName = rs.getString("stuName")

String stuSex = rs.getString("stuSex")

int stuSeat = rs.getInt("stuSeat")

s = new StuInfo()

s.setStuId(stuID)

s.setStuName(stuName)

s.setStuSex(stuSex)

s.setStuSeat(stuSeat)

}

} catch (SQLException e) {

e.printStackTrace()

} finally {

DBHelper.closeResource(rs, ps, conn)

}

return s

}

/**

* 3.添加学生信息

*

* @param s

* @return

*/

public int addStuInfo(StuInfo s) {

int count = 0

conn = DBHelper.getConnection()

String sql = "insert into stuInfo values(?,?,?)"

try {

ps = conn.prepareStatement(sql)

ps.setString(1, s.getStuName())

ps.setString(2, s.getStuSex())

ps.setInt(3, s.getStuSeat())

count = ps.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

} finally {

DBHelper.closeResource(rs, ps, conn)

}

return count

}

/**

* 4.根据学好删除学生信息

*

* @param stuID

* @return

*/

public int delStuInfo(int stuID) {

int count = 0

conn = DBHelper.getConnection()

String sql = "delete from stuInfo where stuId = ?"

try {

ps = conn.prepareStatement(sql)

ps.setInt(1, stuID)

count = ps.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

} finally {

DBHelper.closeResource(rs, ps, conn)

}

return count

}

/**

* 5.根据学号修改学生信息

*

* @param s

* @return

*/

public int updateStuInfo(StuInfo s) {

int count = 0

conn = DBHelper.getConnection()

String sql = "update StuInfo set stuName = ?,stuSex = ?,stuSeat = ? where stuId = ?"

try {

ps = conn.prepareStatement(sql)

ps.setString(1, s.getStuName())

ps.setString(2, s.getStuSex())

ps.setInt(3, s.getStuSeat())

ps.setInt(4, s.getStuId())

count = ps.executeUpdate()

} catch (SQLException e) {

e.printStackTrace()

} finally {

DBHelper.closeResource(rs, ps, conn)

}

return count

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10870103.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存