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