Java编号姓名元宝数密码,通过my Eclipse控制台向数据库(SQL2008)中查找、删除、插入信息...
通過my Eclipse控制臺向數據庫(SQL2008)中查找、刪除、插入信息如果編譯程序有什么錯誤還望大家多多指正代碼執行所需數據庫、架包及java源文件已上傳至文件 文件名 SQl_JDBC.zip用my Eclipse控制臺操作數據庫之前(SQL 2008)之前 應先引入一個架包(sqljdbc4.jar) 在架包導入之后才能通過控制臺操作數據庫com.microsoft.sqlserver.jdbc.SQLServerDriver 在引入架包(sqljdbc4.jar)之后的驅動程序jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123" jdbc:sqlServer: 固定寫法 localhost 本地數據 1433代表SQL的數據端口 databaseName=數據庫名 sa為SQL數據庫登錄名 123為SQL數據庫登錄密碼package jdbchomework;import java.util.Scanner;//測試類文件public class test { //主程序入口 public static void main(String[] args) { //實例化鍵盤輸入 Scanner input=new Scanner(System.in); //實例化操作類 Do a=new Do(); System.out.PRint("請輸入用戶名:"); String username=input.next(); System.out.print("請輸入密碼"); String passWord=input.next(); if(a.found(username, password)){ //輸入用戶名及密碼與數據庫中的用戶名及密碼作比較 System.out.println("登陸成功!歡迎你!"+username);for(;;){ System.out.println("請選擇操作:") System.out.println("1.查詢管理員信息 2.查詢狗狗信息 3.添加狗狗 4.刪除狗狗 0.退出"); System.out.print("請選擇:"); int num=input.nextInt(); switch(num){ case 1: a.show(); //調用操作類 顯示主人信息 break; case 2: a.show1(); //調用操作類 顯示狗狗信息 break; case 3: System.out.print("請輸入狗狗的姓名:"); String name=input.next(); System.out.print("請輸入狗狗的健康值:"); int health=input.nextInt(); System.out.print("請輸入與狗狗的親密度:"); int love=input.nextInt(); System.out.print("請輸入狗狗的品種:"); String strain=input.next(); a.show2(name, health, love, strain); //調用操作類 插入數據 向數據庫Dog中插入數據 break; case 4: System.out.print("請輸入編號:"); int num1=input.nextInt(); a.show3(num1); //調用操作類 刪除數據 刪除數據庫Dog表中id為num1的數據 break; case 0: num=0; break; default: System.out.println("輸入錯誤"); break; } if(num==0){ System.out.println("系統退出,謝謝使用!!!"); break; } } }else{ System.out.println("用戶名或者密碼錯誤"); } }}package jdbchomework;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Scanner;//操作類文件public class Do { //登陸界面 public boolean found(String username,String password){ boolean find=false; //加載驅動 try { // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //建立連接 Connection cn=null; //聲明連接對象 PreparedStatement ps=null; //操作對象 ResultSet rs=null; //結果集對象 try { // cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123"); // String sql="select * from Admin where userName='"+username+"' and pwd='"+password+"'"; String sql="select * from Admin where userName=? and pwd=?"; ps=cn.prepareStatement(sql); ps.setString(1, username); ps.setString(2, password); rs=ps.executeQuery(); if(rs.next()){ find=true; }else{ find=false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ if(ps!=null){ ps.close(); } if(cn!=null){ cn.close(); } }catch(SQLException e){ e.printStackTrace(); } return find; } //顯示主人信息 public void show(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //建立連接 Connection cn=null; PreparedStatement ps=null; ResultSet rs=null; try { cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123"); // String sql="select * from Admin where userName='"+username+"' and pwd='"+password+"'"; String sql="select * from Admin "; ps=cn.prepareStatement(sql); rs=ps.executeQuery(); System.out.println("主人信息列表"); System.out.println("編號\t姓名\t元寶數"); while(rs.next()){ int num=rs.getInt(1); String name=rs.getString(2); int money=rs.getInt(3); System.out.println(num+"\t"+name+"\t"+money); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(cn!=null){ cn.close(); } }catch(SQLException e){ e.printStackTrace(); } } //顯示狗狗信息 public void show1(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //建立連接 Connection cn=null; PreparedStatement ps=null; ResultSet rs=null; try { cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123"); // String sql="select * from Admin where userName='"+username+"' and pwd='"+password+"'"; String sql="select * from Dog "; ps=cn.prepareStatement(sql); rs=ps.executeQuery(); System.out.println("狗狗信息列表"); System.out.println("編號\t姓名\t健康值\t親密度\t品種"); while(rs.next()){ int num=rs.getInt(1); String name=rs.getString(2); int health=rs.getInt(3); int love=rs.getInt(4); String strain=rs.getString(5); System.out.println(num+"\t"+name+"\t"+health+"\t"+love+"\t"+strain); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(cn!=null){ cn.close(); } }catch(SQLException e){ e.printStackTrace(); } } //插入數據 public void show2(String name,int health,int love, String strain ){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Connection cn=null; PreparedStatement ps=null; try { cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123"); String sql="insert Dog values(?,?,?,?)"; ps=cn.prepareStatement(sql); ps.setString(1, name); ps.setInt(2, health); ps.setInt(3, love); ps.setString(4, strain); int i=ps.executeUpdate(); if(i>0){ System.out.println("恭喜你,添加成功!!!"); }else{ System.out.println("添加失敗!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(ps!=null){ ps.close(); } if(cn!=null){ cn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //刪除數據 public void show3(int num){ //加載驅動 try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Connection cn=null; PreparedStatement ps=null; try { cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123"); String sql="delete Dog where id=?"; ps=cn.prepareStatement(sql); ps.setInt(1, num); int i=ps.executeUpdate(); if(i>0){ System.out.println("恭喜你,刪除成功!!!"); }else{ System.out.println("刪除失敗,你輸入的ID不存咋!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //釋放連接 try { if(ps!=null){ ps.close(); } if(cn!=null){ cn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
總結
以上是生活随笔為你收集整理的Java编号姓名元宝数密码,通过my Eclipse控制台向数据库(SQL2008)中查找、删除、插入信息...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nodejs mysql事务处理_关于N
- 下一篇: bootsrap 外边距_Bootstr