eclipse连接mysql数据库basedao怎么写

eclipse连接mysql数据库basedao怎么写,第1张

eclipse连接mysql数据库basedao怎么写

jdbc连接mysql数据库的url为:

jdbc:mysql://主机名或IP抵制:端口号/数据库名?useUnicode=true&characterEncoding=UTF-8

jdbc连接其他数据库的连接字符串写法

这是链接数据库的完整代码,我试过了可以的:

/*

* 注意:1:在创建数据表时应运用这个语句 "st.executeUpdate("use xxsx")"。

* 2:在进行删除,插入,修改时应屏蔽创建数据库,数据表语句,这样可以避免重复创建。

*/

import java.sql.*

public class createdb {

public static void main(String[] args) {

Connection connection

try

{

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

String dbURL="jdbc:mysql://localhost:3306"

connection=DriverManager.getConnection(dbURL,"root","test")

Statement st=connection.createStatement()

String createdatabase="create database kkk"

st.executeUpdate(createdatabase)

st.executeUpdate("use kkk")//必须加上

String createtable="create table QQtable(No CHAR(20) not null,PASSWORD VARCHAR(10) not null,primary key(No) )"

st.executeUpdate(createtable)

String insert="insert into QQtable values('0','zhao')"

st.executeUpdate(insert)

String insert1="insert into QQtable values('1','zxr')"

st.executeUpdate(insert1)

//String update="Update QQtable set No='a' where No='0'"

//st.executeUpdate(update)

// String del = "delete from QQtable where No='1'"

//st.executeUpdate(del)

ResultSet rs=st.executeQuery("SELECT * FROM QQtable")

st.close()

rs.close()

connection.close()

}catch(ClassNotFoundException e)

{

System.out.println("Database driver not found")

}catch(SQLException e)

{

System.out.println(e)

}

}

}

这是MysQL驱动的配置路径:C:\Program Files\Java\jdk1.6.0_14\jre\lib\ext

另外还需要在你的项目名称上右击配置;

这是空指针异常,说明你链接数据库的时候,某个连接没有拿到值,而你用那个连接调用了方法,你可以到tomcat目录下去找,在work下找到你的java类或者设置断点,这个是最好的方法 因为只要是断点走过的地方,变量都给了值。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5897412.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-07
下一篇 2023-03-07

发表评论

登录后才能评论

评论列表(0条)

保存