java mysql2_java连接mysql-2

数组与数据集

代码:

/* 创建表的SQL语句

use test;

create table empinfo

(

no varchar(10),

name varchar(10),

gender varchar(10),

age varchar(10),

dept varchar(10)

);

*/

package pa;

import java.io.*;

import java.sql.*;

public class MainDemo

{

static String[] EmpInfo;

public static void getInfo()

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try

{

EmpInfo=new String[5];

System.out.println("请输入员工号:");

EmpInfo[0]=new String(br.readLine().trim());

System.out.println("请输入姓名:");

EmpInfo[1]=new String(br.readLine().trim());

System.out.println("请输入性别:");

EmpInfo[2]=new String(br.readLine().trim());

System.out.println("请输入年龄:");

EmpInfo[3]=new String(br.readLine().trim());

System.out.println("请输入部门:");

EmpInfo[4]=new String(br.readLine().trim());

}

catch(IOException e)

{

e.printStackTrace();

}

}

public static void insertInfo()

{

Connection conn=null;

PreparedStatement pstmt=null;

try

{

Class.forName("com.mysql.jdbc.Driver");

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","xhp");

String sql="insert into empinfo values(?,?,?,?,?)";

pstmt=conn.prepareStatement(sql);

pstmt.setString(1,EmpInfo[0]);

pstmt.setString(2,EmpInfo[1]);

pstmt.setString(3,EmpInfo[2]);

pstmt.setString(4,EmpInfo[3]);

pstmt.setString(5,EmpInfo[4]);

int result=pstmt.executeUpdate();

if(result==1)

System.out.println("信息已经保存");

else

System.out.println("信息保存失败");

conn.close();

pstmt.close();

}

catch(ClassNotFoundException e)

{

e.printStackTrace();

}

catch(SQLException e)

{

System.out.println("信息保存失败");

e.printStackTrace();

}

finally

{

try

{

if(conn!=null)

{

conn.close();

}

if(pstmt!=null)

{

pstmt.close();

}

}

catch(SQLException ex)

{

ex.printStackTrace();

}

}

}

public static void showInfo()

{

{

Connection conn=null;

Statement stmt=null;

ResultSet rs=null;

try

{

Class.forName("com.mysql.jdbc.Driver");

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","xhp");

stmt=conn.createStatement();

String sql="select * from empinfo";

rs=stmt.executeQuery(sql);//获得结果集的元数据

ResultSetMetaData rsmd=rs.getMetaData();

int columnsNum=rsmd.getColumnCount();

System.out.print("Result:/n");

System.out.print("no /t name /t gender /t age/t dept/n");

while(rs.next())

{

for(int i=0;i

{

String data=rs.getString(i+1);

System.out.print(data+"/t");

}

System.out.print("/n");

}

conn.close();

stmt.close();

}

catch(ClassNotFoundException e)

{

e.printStackTrace();

}

catch(SQLException e)

{

e.printStackTrace();

}

finally

{

try

{

if(conn!=null)

{

conn.close();

}

if(stmt!=null)

{

stmt.close();

}

}

catch(SQLException ex)

{

ex.printStackTrace();

}

}

}

}

public static void main(String[] args)

{

//getInfo();

//insertInfo();

//showInfo();

}

}

使用参数列表实现数据的获取

运行结果:

C:/>javac MainDemo.java

C:/>java MainDemo

请输入员工号: a

请输入姓名: s

请输入性别: d

请输入年龄: f

请输入部门: g

信息已经保存

Result:

no name gender age dept

1 2 3 4 5

0 9 8 7 k

2 3 4 5 6

s d f h j

a s d f g

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值