java新手笔记34 连接数据库

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();

	}

}
时间: 2024-10-13 17:12:39

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

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

1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = false; //跳出方法 同步锁 public synchronized void jump() { while(true) { if(!isLive ) { System.out.println("跳出第 " + index + " 田鼠"); //修改田鼠状态

java新手笔记10 构造器

1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java.util.Random; public class Demo1 { /** * 模拟摇奖 */ public static void main(String[] args) { Random ran = new Random(); int[] a = new int[7]; System.out.p

java新手笔记15 多态

1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println("动物叫..."); } } 2.Dog/Cat/Bird类 package com.yfs.javase; public class Dog extends Animal { public void cry() { System.out.println("汪 汪..."); }

java新手笔记1

//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { System.out.println("Hello World!"); } } //编译命令 C:\>javac C:\Users\Administrator\Desktop\Hello.java //javac 路径+文件名.java //javac -d D:\ C:\Users\

java新手笔记16 面积

1.图形类 package com.yfs.javase; public class Shape { //计算面积方法 public double getArea() { System.out.println("计算面积"); return 0; } } 2.圆 package com.yfs.javase; public class Circle extends Shape { private double r; public Circle(double r) { this.r =

java新手笔记9

1.bank类 package com.yfs.javase; //类封装 public class BankCard { //属性 int balance;//默认0 实例变量 分配给每个对象一个 //String name; // 方法 存钱 public void saveMoney(int money) { if(money < 0) { System.out.println("非法操作..."); return; } System.out.println("向

java新手笔记2

1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void main(String[] args) { System.out.println("World World!"); //System.out.println("welcome java world!"); //注释的代码不执行 单行注释 /* 多行注释 System.

java新手笔记3

1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.println("a = " + a); //a = -a; //+ - System.out.println("a = " + a); //+ 字符串链接 System.out.println("影分身" + "软件开发"); Sys

java新手笔记23 异常

1.import package com.yfs.javase; import java.util.Scanner; //import java.lang.String;//默认导入 public class Demo1 { public static void main(String[] args) { String s = new String("abc");//java.lang.String String s1 = "abc"; System.out.pri