| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:请帮忙看看连接数据库的问题...
只看楼主 加入收藏
Sunny_kaka
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-8-19
收藏
 问题点数:0 回复次数:2 
请帮忙看看连接数据库的问题...
JAVA高手路过请不要走了..一定要帮小弟看看.郁闷了今天

       我用JDBC连接上了SQL SERVER 2005.
       我连接上了数据库.然后运行一遍自己编的程序.完全没问题
        问题是然后我试着在程序的SQL语句里改代码,发现完全没用...
       比如说我在SQL语句里把创建student表的改成创建teacher表.运行后发现..
       完全没用.机器依旧运行的是原来的那个程序..也就是又创建了student表.
       到底什么原因?
       实在不明白了..
       我快抓狂了..   : (

代码如下
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Date;
import java.util.Properties;
import
import
import

public class ORACLECreatTableTest
{
        private Connection conn;
        private String url;//数据库URL
        private String userName;//登录数据库用户名
        private String password;//用户密码
        
        public static void main(String[] args)
        {               
                CreatTableTest test = new CreatTableTest();
                test.getProperty();               
                Connection conn = test.getConnection();
                test.createTable(conn);
                test.getStudent(conn);
               
        }
        
        /**
         * 在数据库studentmanager中创建一个表student,并向表中插入一条记录
         */
        public void createTable(Connection conn)
        {
                try
                {
                        Statement st = conn.createStatement();
                        String sql = "CREATE TABLE STUDENT1(姓名 varchar(12) NOT NULL," +
                                                 "学号 varchar(10) NOT NULL," +
                                                 "出生日期 datetime NOT NULL,专业 varchar(10) NULL," +
                                                 "籍贯 varchar(30) NULL)";
                        System.out.println("输出的SQL语句是:");
                        System.out.println(sql);
                        st.execute(sql);
                        
                        sql = "insert into student1 values('王成','20021022','1977-6-10', '英语','河北')";
                        st.executeUpdate(sql);
                        st.close();
                                
                }
                catch(SQLException e)
                {
                        e.printStackTrace();
                }
        }
        
        /**
         * 从表中查询出所有记录
         */
        public void getStudent(Connection conn)
        {
                try
                {
                        Statement st = conn.createStatement();
                        String sql = "select * from student";
                        ResultSet rs = st.executeQuery(sql);
                        
                        while(rs.next())
                        {
                                String name = rs.getString("姓名");
                                String number = rs.getString("学号");
                                Date date = rs.getDate("出生日期");
                                String spe = rs.getString("专业");
                                String address = rs.getString("籍贯");
                                
                                System.out.println( "\n姓名:" + name + "\t学号:" + number +
                                                                        "\t出生日期:" + date + "\t专业:" +
                                                                        spe + "\t籍贯:" + address );                                 
                        }
                        
                        st.close();
                        conn.close();               
                }
                catch(SQLException e)
                {
                        e.printStackTrace();
                }
        }
        
        
        /**
         * 返回到数据库的一个连接,在一个系统或类中,如果经常进行数据库的相关操作
         * 会把建立数据库的连接作为一个单独的方法。
         */
        public Connection getConnection()
        {
                try
                {
                        conn = DriverManager.getConnection(url, userName, password);
                }
                catch(SQLException e)
                {
                        e.printStackTrace();
                }
                                
                return conn;
        }
        
        /**
         * 读取属性配置文件
         */
        public void getProperty()
        {
                Properties prop = new Properties();
                try
                {
                        FileInputStream in = new FileInputStream("Driver2.properties");
                        prop.load(in);
        
                        String driver = prop.getProperty("drivers");
                        if(driver != null)
                                System.setProperty("jdbc.drivers", driver);
                        url = prop.getProperty("url");
                        userName = prop.getProperty("user");
                        password = prop.getProperty("password");                        
                }
                catch(FileNotFoundException e)
                {
                        e.printStackTrace();
                }
                catch(IOException e)
                {
                        e.printStackTrace();
                }
        }
}
搜索更多相关主题的帖子: 数据库 
2008-09-24 21:43
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
收藏
得分:0 
我感觉可能是你的SQL 语句有问题
2008-09-24 22:29
Sunny_kaka
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-8-19
收藏
得分:0 
不会吧``没问题类
2008-09-24 23:23
快速回复:请帮忙看看连接数据库的问题...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018332 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved