1.JdbcUtil

package com.yfs.javase.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JdbcUtil {
private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=school.mdb"; // 获取连接方法
public static Connection getConnection() {
Connection conn = null;
// 连接数据库
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
System.out.println("驱动类找不到...");
} catch (SQLException e) {
System.out.println("连接数据库失败...");
}
return conn;
} // 释放连接
public static void release(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} }

2.连接信息:jdbc.properties文件

##oracle\u9A71\u52A8
jdbc.driver = oracle.jdbc.OracleDriver
jdbc.url = jdbc:oracle:thin:@YEMA203-13:1521:YFS_DB
jdbc.username = scott
jdbc.password = tiger ##SQL Server 2005
#jdbc.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbc.url = jdbc:sqlserver://localhost:1434;databaseName=YFS_DB
#jdbc.username = sa
#jdbc.password = ##MySQL
#jdbc.driver = com.mysql.jdbc.Driver
#jdbc.url = jdbc:mysql://localhost:3306/test
#jdbc.username = root
#jdbc.password = root ##Access
#jdbc.driver = sun.jdbc.odbc.JdbcOdbcDriver
#jdbc.url = jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=YFS_DB.mdb "... DBQ="+application.getRealPath("/Data/ReportDemo.mdb")
#jdbc.username =
#jdbc.password =

3.JdbcDemo1测试sql执行(增,查,改,删)

package com.yfs.javase.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; public class JdbcDemo1 { public static void main(String[] args) throws Exception {
//createTable();
//insertData();
//upData();
//deleteData();
create();
} public static void create() throws Exception {
Connection conn = JdbcUtil.getConnection();
Statement st = conn.createStatement();
// 执行的sql语句
String sql = "create table stu(id int primary key, name char(30), score int)";
// 执行
st.execute(sql);
// 关闭连接
JdbcUtil.release(null, st, conn);
System.out.println("表创建成功..."); } public static void deleteData() throws Exception {
Connection conn = JdbcUtil.getConnection();
Statement st = conn.createStatement();
// 执行的sql语句
String sql = "delete from stu where id=102";
// 执行
st.execute(sql);
// 关闭连接
JdbcUtil.release(null, st, conn);
System.out.println("删除数据成功..."); } public static void upData() throws Exception {
Connection conn = JdbcUtil.getConnection();
Statement st = conn.createStatement();
// 执行的sql语句
String sql = "update stu set score=80 where id=102";
// 执行
st.execute(sql);
// 关闭连接
st.close();
conn.close();
System.out.println("更新数据成功..."); } public static void insertData() throws Exception {
// 加载驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// url
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:/school.mdb";
// 获取连接
Connection conn = DriverManager.getConnection(url);// url 数据库地址
// 执行对象
Statement st = conn.createStatement();
// 执行的sql语句
//String sql = "insert into stu(id,name,score) values(101,'野马',98)";
String sql = "insert into stu(id,name,score) values(102,'变形金刚4',65)";
// 执行
st.execute(sql);
// 关闭连接
st.close();
conn.close();
System.out.println("插入数据成功..."); } public static void createTable() throws Exception {
// 加载驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// url
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:/school.mdb";
// 获取连接
Connection conn = DriverManager.getConnection(url);// url 数据库地址
// 执行对象
Statement st = conn.createStatement();
// 执行的sql语句
String sql = "create table stu(id int primary key, name char(30), score int)";
// 执行
st.execute(sql);
// 关闭连接
st.close();
conn.close();
System.out.println("数据库操作成功...");
} }

4.Server3

package com.yfs.javase;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; public class Server3 { /**
* 启动服务 监听端口
*/
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(3000);
// 启动
System.out.println("服务器启动,监听3000端口...");
final Socket socket = server.accept();// 监听是否有其他主机连接
String other = socket.getInetAddress().getHostAddress();
System.out.println(other + "请求连接...");
// 接收信息 启动多线程
new Thread(new Runnable() {
@Override
public void run() {
InputStream in = null;
BufferedReader read = null;
while (true) {
try {
in = socket.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String msg = read.readLine();
if (msg != null) {
System.out.println("客户端说 : " + msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start(); // 发送信息 获取输出流
OutputStream out = socket.getOutputStream();
BufferedOutputStream buf = new BufferedOutputStream(out);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(buf),true);
pw.println("这是服务器发送的信息....");
Scanner scan = new Scanner(System.in);
String msg = scan.next();
while (!msg.equals("bye")) {
pw.println(msg);
msg = scan.next();
}
pw.close();
buf.close();
out.close();
System.out.println("信息发送完成"); } }

4.Client3

package com.yfs.javase;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner; public class Client3 { /**
* @param args
*/
public static void main(String[] args) throws Exception {
final Socket socket = new Socket("192.168.1.30", 3000);
// 接收信息
new Thread(new Runnable() {
@Override
public void run() {
InputStream in = null;
BufferedReader read = null;
try {
in = socket.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
String msg = read.readLine();
while (msg != null) {
System.out.println("服务器说:" + msg);
msg = read.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start(); // 发送信息
OutputStream out = socket.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out), true);
Scanner scan = new Scanner(System.in);
String msg = scan.next();
while (!msg.equals("bye")) {
pw.println(msg);
msg = scan.next();
}
out.close();
pw.close(); } }

java新手笔记34 连接数据库的更多相关文章

  1. JAVA新手笔记 Intent对象和Bundle对象

    Intent对象和Bundle对象 功能主要是在 MainActivity中定义了2个EditText,当用户输入内容,把他传入到第二个活动, 自己新创的活动中,MyActivity中 放在MainA ...

  2. java新手笔记33 多线程、客户端、服务器

    1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = ...

  3. java新手笔记32 jdk5新特性

    1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...

  4. java新手笔记31 集合实现类

    Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...

  5. java新手笔记30 集合

    1.set/list package com.yfs.javase; import java.util.ArrayList; import java.util.Collection; import j ...

  6. java新手笔记29 读取文件

    1.读取文件 package com.yfs.javase; import java.io.FileInputStream; import java.io.FileReader; import jav ...

  7. java新手笔记28 文件

    1.目录.文件 package com.yfs.javase; import java.io.File; public class FileDemo1 { /** * File 目录 文件 */ pu ...

  8. java新手笔记27 监听器类

    1.外部类监听 package com.yfs.javase; import java.awt.Button; import java.awt.FlowLayout; import java.awt. ...

  9. java新手笔记26 Frame

    0.Calculater package com.yfs.javase; import java.awt.BorderLayout; import java.awt.Button; import ja ...

随机推荐

  1. Win7 不能安装 msi 解决办法

    Win7 不能安装Setup.msi解决办法 解决方案如下: 新建一个文本文件,输入msiexec /i d:\Setup.msi (假设文件名为Setup.msi ,放在d盘根目录下,即是安装程序的 ...

  2. 【Objective-C】04-第一个OC程序解析

    说明:这个Objective-C专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序猿,可以高速上手Objective-C.假设你还没有编程经验,或者对Objective-C.iOS开发 ...

  3. delphi Sender和Tag的用法1

    Sender和Tag的用法  在它们共同的OnClick事件下返回单击的那个按钮的标题         unit Unit1;interfaceuses  Winapi.Windows, Winapi ...

  4. cardsui-for-android

    https://github.com/Androguide/cardsui-for-android cardsui-for-android-master.zip

  5. Android短信彩信收发流程(应用层)

    下图为ComposeMessageActivity中confirmSendMessageIfNeeded部分的信息发送流程.主要以接收者有效性的确认为主,然后转向sendMessage方法进行发送. ...

  6. MYSQL分页limit速度太慢优化方法

    http://www.fienda.com/archives/110 在mysql中limit可以实现快速分页,但是如果数据到了几百万时我们的limit必须优化才能有效的合理的实现分页了,否则可能卡死 ...

  7. online ddl 跟踪

    | >mysql_prepare_alter_table T@4 : | | | | | | <mysql_prepare_alter_table 7368 T@4 : | | | | | ...

  8. 通过tracing技术来教授操作系统

    https://www.cl.cam.ac.uk/teaching/1516/L41/materials.html https://github.com/myaut/dtrace-stap-book

  9. mha 自动failover 原创

    自动failover slave1:stop slave io_thread slave2stop slave io_thread server1:   create database sbtest; ...

  10. C#多线程的介绍(园子里比较全的一篇)

    一.多线程的概念  Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一 ...