<connectionStrings>
<add name="dbConnection" connectionString="Data Source=192.168.1.100Initial Catalog=NanFangMgrUser ID=saPassWord=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
2.在C#中调用:
System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString
3.将上述连接串保存到文本文件中
private string FILE_NAME = Application.StartupPath + "\\mytxtFile.txt"
private void WriteFile(string str)
{
StreamWriter sr
if (File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象
{
sr = File.AppendText(FILE_NAME)
}
else//如果文件不存在,则创建File.CreateText对象
{
sr = File.CreateText(FILE_NAME)
}
sr.WriteLine(str)
sr.Close()
}
4.从文本文件中去内容
private String ReadTxtFile()
{
if (File.Exists(FILE_NAME)) //如果文件存在
{
String[] strs = System.IO.File.ReadAllLines(FILE_NAME)
return strs[strs.Length - 1]
}
return String.Empty
}
5.数据库连接,并 *** 作
5.1 查询
String ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString
public DataTable Query(String where)
{
String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace("", "").Replace("--", "").Replace("exec", ""))
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(ConnectionString))
DataTable dt = new DataTable()
da.Fill(dt)
return dt
}
catch
{
return null
}
}
5.2 新增
public int New(Entities.mytable obj)
{
String sql = "insert into mytable(pkid,a,b,c) values(@pkid,@a,@b,@c)"
SqlConnection cn = new SqlConnection(ConnectionString)
SqlCommand cmd = new SqlCommand(sql, cn)
cmd.Parameters.AddWithValue("@a", obj.a)
cmd.Parameters.AddWithValue("@b", obj.b)
cmd.Parameters.AddWithValue("@c", obj.c)
cmd.Parameters.AddWithValue("@pkid",
String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid)
try
{
if (cn.State != ConnectionState.Open)
cn.Open()
return cmd.ExecuteNonQuery()
}
catch
{
return -1
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close()
}
}
5.3 编辑
public int Update(Entities.mytable obj)
{
String sql = "Update mytable Set a=@a,b=@b,c=@c Where pkid=@ObjectID"
SqlConnection cn = new SqlConnection(ConnectionString)
SqlCommand cmd = new SqlCommand(sql, cn)
cmd.Parameters.AddWithValue("@a", obj.a)
cmd.Parameters.AddWithValue("@b", obj.b)
cmd.Parameters.AddWithValue("@c", obj.c)
cmd.Parameters.AddWithValue("@pkid", obj.pkid)
try
{
if (cn.State != ConnectionState.Open)
cn.Open()
return cmd.ExecuteNonQuery()
}
catch
{
return -1
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close()
}
}
5.4 删除
public int Del(String where)
{
String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""))
SqlConnection cn = new SqlConnection(ConnectionString)
SqlCommand cmd = new SqlCommand(sql, cn)
try
{
if (cn.State != ConnectionState.Open)
cn.Open()
return cmd.ExecuteNonQuery()
}
catch
{
return -1
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close()
}
}
先看数据库表, 我里面有46条记录,其中有三条重复,我就拿其中一条emp_id 为"
DWR65030M" 做例子
java代码如下:
import java.io.Fileimport java.io.FileNotFoundException
import java.io.FileWriter
import java.io.IOException
import java.io.PrintWriter
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
/**
* java读取数据库内容并存放到文件中
*
* @author young
*
*/
public class JavaToSQLTest {
public static void main(String[] args) throws FileNotFoundException {
// // 关联文件
// File file = new File("F:\\workspace\\one\\test.txt")
// // java IO流和文件关联
// PrintWriter pw = new PrintWriter(file)
PrintWriter pw = null
FileWriter fw = null
// 定义数据库驱动
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
// 数据库连接URL
String url = "jdbc:sqlserver://localhost:1433DatabaseName = soft"
Connection conn = null
String id
String fname, lname
try {
// pw.println("emp_id\t\tfname\t\tlname")
// pw.println("------\t\t------\t\t------")
// 加载数据库驱动
Class.forName(driver)
// 创建数据库连接
conn = DriverManager.getConnection(url, "sa", "1234")
// 创建预编译SQL对象
PreparedStatement ps = conn
.prepareStatement("select emp_id, fname, lname from emps")
// 执行SQL,获取结果集rs
ResultSet rs = ps.executeQuery()
// 处理结果集
while (rs.next()) {
id = rs.getString("emp_id")
fname = rs.getString("fname")
lname = rs.getString("lname")
String filename = id + ".txt"
// 关联文件
File file = new File(filename)
if(!file.exists()){
// 判断文件不存在就new新文件,写数据
try {
file.createNewFile()
// java IO流和文件关联
pw = new PrintWriter(file)
pw.print(id + "\t")
pw.print(fname + "\t\t")
pw.print(lname)
pw.println()
pw.flush()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}else{
// 判断文件存在,就以FileWriter文件追加的方式写文件
try {
fw = new FileWriter(filename,true)
fw.write(id + "\t")
fw.write(fname + "\t\t")
fw.write(lname)
fw.flush()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
System.out.println("加载数据库失败")
System.exit(1)
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
System.out.println("数据库连接错误")
System.exit(1)
} finally {
if (conn != null) {
try {
// 关闭数据库连接
conn.close()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
if (pw != null) {
// 关闭IO流
pw.close()
}
if(fw != null){
try {
fw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
}
结果生成了43个txt文件 ,看
DWR65030M.txt文件的内容如下:
里面有两条记录 ,实现了
发现java和mysql的jdbc驱动两方面的代码,在jdk1.7的版本中,有长度为long类型的方法。而对应的mysql的jdbc驱动jar中,还没有实现long类型方法。
将其进行类型强制转换后,即可正常运行。如下:
pstmt.setAsciiStream(5, inputStream,(int)context.length())
这样即可插入到数据库中。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)