java怎么把从文件读数据改成从数据库读数据?

正是七夕圆月 2017-05-10 11:19:01
就是下面这段代码,是从.txt读取数据的,求问怎么改成从MySQL数据库读数据?
//从文件input.txt读数据
PrintWriter out=new PrintWriter(new FileWriter("out.txt"));
String ecoding = "gbk";
File file = new File("input.txt");
if(file.isFile() && file.exists() ){
InputStreamReader read = new InputStreamReader( new FileInputStream(file),ecoding);
BufferedReader bufferedreader = new BufferedReader(read);
String Text_line = null;
Text_line = bufferedreader.readLine();
String[] max_str=Text_line.split("-");
max=Integer.parseInt(max_str[0]);

while( ( Text_line = bufferedreader.readLine() ) != null ){
String text = Text_line.replace(",", "");
String[] version=text.split("-");

for(int k = 0; k < version.length; k++ ){
String[] version2 = version[k].split(":");
int x1=Integer.parseInt(version2[0]);
int x2=Integer.parseInt(version2[1]);
v_side v_sd = new v_side(x1-1,x2-1,1);
vec_relationship.add(v_sd);
}
}
read.close();
bufferedreader.close();
}

r_sign = new int[max];//给r_sign设置数组大小
//r_sign数组先统一赋值为-1
for(i = 0; i < max; i++){
r_sign[i] = -1;
}
GN_use.v_order( vec_relationship, r_sign);//对vec_relationship中的内容进行排序整理
...全文
190 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
一个治疗术 2017-05-11
  • 打赏
  • 举报
回复
 // 驱动程序名
           String driver = "com.mysql.jdbc.Driver";

           // URL指向要访问的数据库名scutcs
           String url = "jdbc:mysql://127.0.0.1:3306/test";

           // MySQL配置时的用户名
           String user = "root";
 
           // MySQL配置时的密码
           String password = "123456";

           try {
            // 加载驱动程序
            Class.forName(driver);

            // 连续数据库
            Connection conn = DriverManager.getConnection(url, user, password);

            if(!conn.isClosed())
             System.out.println("Succeeded connecting to the Database!");

            // statement用来执行SQL语句
            Statement statement = conn.createStatement();

            // 要执行的SQL语句
            String sql = "select * from pet";

            // 结果集
            ResultSet rs = statement.executeQuery(sql);

            System.out.println("-----------------");
           

            String name = null;

            while(rs.next()) {
   
             // 选择sname这列数据
             name = rs.getString("name");
   
             // 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
             // 然后使用GB2312字符集解码指定的字节数组
             name = new String(name.getBytes("ISO-8859-1"),"GB2312");

             // 输出结果
             System.out.println(rs.getString("ID") + "\t" + name);
            }

            rs.close();
            conn.close();

           } catch(ClassNotFoundException e) {


            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();


           } catch(SQLException e) {


            e.printStackTrace();


           } catch(Exception e) {


            e.printStackTrace();


           }

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧