java新手笔记34 连接数据库

时间:2021-01-28 23:59:13

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. epoll ET模式陷阱分析

    0. 前言 这篇文章主要记录在使用epoll实现NIO接入时所遇到的问题. 1. epoll简介 epoll是Linux下提供的NIO,其主要有两种模式,ET(Edge trige)和LT(Level ...

  2. 循环语句--for

    1.guess_age优化版v1.py #coding=utf-8 age = 22 for i in range(10): if i < 3: guess_num = int(input('i ...

  3. TCP、UDP协议间的区别(转)

    一.TCP/IP协议是一个协议簇.里面包括很多协议的.UDP只是其中的一个.之所以命名为TCP/IP协议,因为TCP,IP协议是两个很重要的协议,就用他两命名了. TCP/IP协议集包括应用层,传输层 ...

  4. Senparc&period;Weixin&period;MP&period;Sample 配置redis服务器密码

    redis.windows-service.conf中加  requirepass 你的密码 <!-- Cache.Redis连接配置 --> <add key="Cach ...

  5. ArrayList等常见集合的排序问题

    对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体. 主要用Collections.sort方法对集合类中的对象进行排序 Collect ...

  6. sqlserver,sqlite,access数据库链接字符串

    SqlServer:string connection = "server=32.1.1.48;database=数据库名;user=sa;password=sa2008"; ac ...

  7. 关于SIGPIPE导致的程序退出

    http://www.cppblog.com/elva/archive/2008/09/10/61544.html 收集一些网上的资料,以便参考: http://blog.chinaunix.net/ ...

  8. QtWebkit2&period;2&period;0 HTML5&period;0支持情况

      Canvas: 支持element, 2d context以及文本 解析规则:支持 HTML5 tokenizer/tree building,  SVG in text/html, MathML ...

  9. Google 浏览器好用插件推荐

    Adblock Plus, 免费广告拦截器 Adobe Acrobat  将当前网页转换为 Adobe PDF 文件 Axure RP Extension for Chrome  不仅能绘制出详细的产 ...

  10. Go url编码和字符转码

    类似php中的urlencode 和htmlspecialchars: package main import ( "fmt" "html" "net ...